I stumbled upon this odd result when I was messing around with C arrays:
char s[100] = "hello";
if(s == &s[0]) printf("true. ");
if(s == &s) printf("true.");
// output: true. true.
I know that s
holds the memory location of the first element, but is there a way to find the address of s
(the address of the pointer that points to the first element)? Why does &s
equal s
? Not that this is useful, but I'd like to know what's going on under the hood.
I'm not sure if different compilers implement it differently, but I am using gcc.
If I told you to put your finger on the place in memory where the character array s is located, and then where the first character in the array is located, wouldn't they be the same?
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