0

Quention on C

Why does the following piece of code display 8 the first time, but more the second. I do know that the fjrst time it acts like a pointer, but don't know anything about the second. https://code.sololearn.com/c5865583k8B8/?ref=app

7th Apr 2019, 3:11 PM
Yuvraj Singh
Yuvraj Singh - avatar
5 odpowiedzi
+ 4
Because the function doesn't take an array of chars, but a pointer to char as an argument. msg is a pointer to the first element of the char array. *msg and msg[0] are equivalent. You can change the function definition to fortune_cookie(char *msg) and it will work just the same. What the function shows is the size of a char pointer. The size may change depending on the platform, in this case it is 8
7th Apr 2019, 3:22 PM
Anna
Anna - avatar
+ 2
When you pass a C string to a function like this, only a pointer to that string will be passed, and that has 8 bytes.
7th Apr 2019, 3:22 PM
HonFu
HonFu - avatar
+ 1
The second time it shows the actual length of the string from the first character to the closing \0 character
7th Apr 2019, 3:25 PM
Anna
Anna - avatar
+ 1
Oh ok, ty!
7th Apr 2019, 3:26 PM
Yuvraj Singh
Yuvraj Singh - avatar
0
I do know that but why does it display more the second time??
7th Apr 2019, 3:24 PM
Yuvraj Singh
Yuvraj Singh - avatar