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?
In C, if you need to amend a string in a called function, pass a pointer to the first char in the string as an argument to the function. If you have allocated storage for the string outside the function, which you cannot exceed within the function, it's probably a good idea to pass in the size.
String literals are passed to functions as pointers to a stored string. For example, given the statement: printf( "Please enter a positive value for the angle: " );
First, we have the function definition “DisplayString,” where a constant string reference is passed. The constant strings are defined and initialized in the main function as “str1” and “str2”. After that, pass these constant strings to the function “InputString”.
strings are passed by reference. The built in string type is a value type.
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.
"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]
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With