I am reading the chapter on arrays and pointers in Kernighan and Richie\'s The C Programming Language.
They give the example:
/* strlen: return
"hello, world"
is an array of char
(type is char[13]
). The value of an array of char
in an expression is a pointer to char
. The pointer points to the first element of the array (i.e., the value of "hello, world"
is &"hello, world"[0]
).
To understand how a string like "Hello World" is converted to a pointer, it is important to understand that, the string is actually hexadecimal data starting at an address and moving along till it finds a NULL
So that means, every string constant such as "Hello World" is stored in the memory somewhere
Possibility would be:
0x10203040 : 0x48 [H]
0x10203041 : 0x65 [e]
0x10203042 : 0x6C [l]
0x10203043 : 0x6C [l]
0x10203044 : 0x6F [o]
0x10203045 : 0x20 [' ']
0x10203046 : 0x57 [W]
0x10203047 : 0x6F [o]
0x10203048 : 0x72 [r]
0x10203049 : 0x6C [l]
0x1020304A : 0x64 [d]
0x1020304B : 0x00 [\0]
So, when this function is called with the above values in the memory, [left side is address followed by ':' and the right side is ascii value of the character]
int strlen(const char *s)
{
int n;
for (n = 0; *s != ′\0′; s++)
n++;
return n;
}
strlen("Hello World");
at that time, what gets passed to strlen
is the value 0x10203040
which is the address of the first element of the character array.
Notice, the address is passed by value.. hence, strlen
has its own copy of the address of "Hello World". starting from n = 0
, following uptil I find \0
in the memory, I increment n
and also the address in s
(which then gets incremented to 0x10203041
) and so on, until it finds \0
at the address 0x1020304B
and returns the string length.
Note that:
"hello, word"
is stored somewhere in memorySo, a pointer could as easily simply point to a static string as to any other (dynamical) structure that is stored in memory (like an array of characters). There is really no difference with the other provided examples.
char str[] = "Hello, world";
strlen(str);
string in C are array of characters with terminating NULL ('\0'), that mean it should be in the memory some where.
so what is the difference of sending a stored string as above and sending it directly as below
strlen("Hello, World");
The answer is both are same, but where the string is stored and how it's handled, here comes the compiler and stack.
The compiler at compile time pushes the string in to stack and send the starting address (char*) in the stack, the calling function sees a pointer and access the string.
The compiler also add codes after exist form the function to restore the stack in the correct place thus deleting the temporary string created Note: the above is implementation dependent of the compiler, but most of the compiler works this way
As it says in the first paragraph of the same page (Page 99, K&R2):
"By definition, the value of a variable or expression of type array is the address of element zero of the array."
The value of "hello, world" would be the address of 'h'.
Does the function assign this string literal as the value of its local variable *s and then use s as the array name/pointer?
Yes