char *'s and const char *'s (followup)
Bruce Worden
bruce at seismo.gps.caltech.edu
Thu Jan 17 10:42:02 AEST 1991
Thanks to everyone who responded here and by e-mail. I got a number of
interesting responses. Doug's was typical:
In article <14825 at smoke.brl.mil> gwyn at smoke.brl.mil (Doug Gwyn) writes:
>In article <1991Jan11.182945.5437 at nntp-server.caltech.edu> bruce at 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 at seismo.gps.caltech.edu
252-21 Seismological Laboratory, Caltech, Pasadena, CA 91125
More information about the Comp.lang.c
mailing list