A
andrew.fabbro
In a different newsgroup, I was told that a function I'd written that
looked like this:
void myfunc (char * somestring_ptr)
should instead be
void myfunc (const char * somestring_ptr)
When I asked why, I was told that it facilitated calling it as:
myfunc ("String");
instead of
strcpy (string, "What I want");
myfunc (string);
However, now that I experiment with it, I find that my original
prototype
void myfunc (char * somestring_ptr)
works perfectly well with either invocation.
So I'm curious why "const char *" would be desirable?
The only difference I see it makes is that it forbids modification of
the string within the function...of course, you can't modify the
original copy of the string, since C is call-by-value, but I guess
const char * means you can't modify the passed copy in the function
either...perhaps this is just an additional safeguard?
I'm curious if there is something I'm missing about "const char *" in
function prototypes. K&R doesn't mention anything about this (though
of course some of the library functions do use "const char *").
Thanks,
-AF
looked like this:
void myfunc (char * somestring_ptr)
should instead be
void myfunc (const char * somestring_ptr)
When I asked why, I was told that it facilitated calling it as:
myfunc ("String");
instead of
strcpy (string, "What I want");
myfunc (string);
However, now that I experiment with it, I find that my original
prototype
void myfunc (char * somestring_ptr)
works perfectly well with either invocation.
So I'm curious why "const char *" would be desirable?
The only difference I see it makes is that it forbids modification of
the string within the function...of course, you can't modify the
original copy of the string, since C is call-by-value, but I guess
const char * means you can't modify the passed copy in the function
either...perhaps this is just an additional safeguard?
I'm curious if there is something I'm missing about "const char *" in
function prototypes. K&R doesn't mention anything about this (though
of course some of the library functions do use "const char *").
Thanks,
-AF