That's because s
is an array in main
, but when you pass it as a parameter in stringLength
it decays into a pointer.
It means that string
is not an array, it is a pointer, which contains the address of s
. The compiler changes it without telling you, so it is equivalent to write the function as:
int stringLength(char *string);
There's a page that talks about C arrays and pointers in the C-Faq
From there, I recommend you to read questions:
- 6.6 Why you can modify
string
in stringLength
- 6.7 Why you can't modify
s
in main
- 6.2 I heard
char a[]
was identical to char *a
- 6.3 what is meant by the ``equivalence of pointers and arrays'' in C?
- 6.4 why are array and pointer declarations interchangeable as function formal parameters?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…