[comp.lang.c] char *'s and const char *'s

bruce@seismo.gps.caltech.edu (Bruce Worden) (01/12/91)

Given:

int foo(char *file);

And knowing that foo() really only needs a const char *, it is often called as:

	a = foo("myfile");

but a kindly compiler will warn something like:

	junk.c:7: warning: argument passing of non-const * pointer from const *

A possible solution is:

	char str[] = "myfile";
	.
	.
	.
	a = foo(str);

but this distances the file name from the function call (a minor 
inconvenience).  The question: What is the method preferred/recommended by
professionals and other experienced individuals?
--------------------------------------------------------------------------
C. Bruce Worden                            bruce@seismo.gps.caltech.edu
252-21 Seismological Laboratory, Caltech, Pasadena, CA 91125

gwyn@smoke.brl.mil (Doug Gwyn) (01/12/91)

In article <1991Jan11.182945.5437@nntp-server.caltech.edu> bruce@seismo.gps.caltech.edu (Bruce Worden) writes:
>Given:
>int foo(char *file);
>And knowing that foo() really only needs a const char *, it is often called as:
>	a = foo("myfile");
>but a kindly compiler will warn something like:
>	junk.c:7: warning: argument passing of non-const * pointer from const *

You should have declared foo as int foo(const char *file).  No diagnostic
should be generated when a pointer to non-const char is passed as an
argument to the function, and of course no diagnostic would be issued
for a pointer to const char either.

steve@taumet.com (Stephen Clamage) (01/13/91)

bruce@seismo.gps.caltech.edu (Bruce Worden) writes:

>int foo(char *file);
>... knowing that foo() really only needs a const char *, it is often called as:
>	a = foo("myfile");  [ but this leads to compiler warnings ]
>What is the method preferred/recommended by professionals and other experienced individuals?

In this case I prefer to declare foo to take a const char*.  Why mislead
the compiler and human readers when it is easy to be helpful?

-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

rjc@uk.ac.ed.cstr (Richard Caley) (01/13/91)

In article <1991Jan11.182945.5437@nntp-server.caltech.edu> bruce@seismo.gps.caltech.edu (Bruce Worden) writes:

    Given:

    int foo(char *file);

    And knowing that foo() really only needs a const char *, it is
    often called as: 

	    a = foo("myfile");

    but a kindly compiler will warn something like:

	    junk.c:7: warning: argument passing of non-const * [...]

    The question: What is the method preferred/recommended by
    professionals and other experienced individuals?

I don't know about the high muckerty muck profesional types, but my
first move would be to shout at the person responsible for the
decalration. 

Of course, you have to be quite sure that the function really does not
do anything to the string. Just because it is obvious it should not,
that does not mean it won't just for the hell of it.

Quick quiz: which three letter workstation manufacturer's `sscanf'
writes to its first argument... Answers on a letter bomb to...

--
rjc@cstr.ed.ac.uk

bruce@seismo.gps.caltech.edu (Bruce Worden) (01/17/91)

Thanks to everyone who responded here and by e-mail.  I got a number of
interesting responses.  Doug's was typical:

In article <14825@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes:
>In article <1991Jan11.182945.5437@nntp-server.caltech.edu> bruce@seismo.gps.caltech.edu (Bruce Worden) writes:
>>Given:
>>int foo(char *file);
>>And knowing that foo() really only needs a const char *, it is often called as:
>>	a = foo("myfile");
>>but a kindly compiler will warn something like:
>>	junk.c:7: warning: argument passing of non-const * pointer from const *
>
>You should have declared foo as int foo(const char *file).  No diagnostic
>should be generated when a pointer to non-const char is passed as an
>argument to the function, and of course no diagnostic would be issued
>for a pointer to const char either.

Which is, of course, the correct approach.  However, when I wrote "Given:" 
I really meant "Given:".  So based on the comments I got, I can nag the 
supplier of the code, a task which I will gleefully undertake.  In the 
meantime I will use:

	char descriptive_name[] = "myfile";

	...
	a = foo(descriptive_name);
	...

As a number of people suggested.  Thanks again.
--------------------------------------------------------------------------
C. Bruce Worden                            bruce@seismo.gps.caltech.edu
252-21 Seismological Laboratory, Caltech, Pasadena, CA 91125