In article <11**********************@z14g2000cwz.googlegroups .com>
G Patel <ga********@gmail.com> wrote:
I've been looking at some code for string functions (certain
implementation of them) and the non modified string is usually declared
as a const char *s in the parameter list.
I was wondering, what exactly does that mean. Does that mean the
function is restricted from changing the address (changing s) or
changing what's being pointed to (*s)?
C's rule is that "declaration mirrors use", so "const char *s"
says that "*s" has type "const char" -- i.e., *s is a read-only
(but not unchanging) char.
See the comp.lang.c FAQ, question 11.9, et seq.
To see that "*s" does indeed change, compile and run the following
small program:
#include <stdio.h>
char buf[30];
void f(const char *);
int main(void) {
strcpy(buf, "hello world");
f(buf);
return 0;
}
void f(const char *s) {
printf("entered f(\"%s\")\n", s);
buf[0] = 'j';
printf("after changing buf[0], we now have \"%s\"\n", s);
}
Neither you nor the compiler can assume that *s, or indeed any
s[i], is unmodified between various statements unless you can prove
that that s[i] is not modified by some *other* assignment.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.