Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oddities in C char arrays

Tags:

arrays

c

pointers

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.

like image 857
Elben Shira Avatar asked Nov 28 '22 19:11

Elben Shira


1 Answers

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?

like image 159
dkretz Avatar answered Dec 17 '22 00:12

dkretz