I am reading the chapter on arrays and pointers in Kernighan and Richie's The C Programming Language.
They give the example:
/* strlen: return length of string s */
int strlen(char *s)
{
int n;
for (n = 0; *s != '\0'; s++)
n++;
return n;
}
And then say:
“Since s
is a pointer, incrementing it is perfectly legal; s++
has no effect on the character string in the function that called strlen
, but merely increments strlen’s private copy of the pointer. That means that calls like
strlen("hello, world"); /* string constant */
strlen(array); /* char array[100]; */
strlen(ptr); /* char *ptr; */
all work.”
I feel like I understand all of this except the first call example: Why, or how, is the string literal "hello, world"
treated as a char *s
? How is this a pointer? Does the function assign this string literal as the value of its local variable *s
and then use s
as the array name/pointer?