int main(int argc, char *argv[])
{
char b[]="Among Us";
char* string2 = &b[0];
printf("Print address of b\t\t= %p\n", &b[0]);
printf("Print content of string2\t= %p\n", string2);
printf("Print content of string2\t= %s\n", string2); // why ???
system("PAUSE");
return 0;
}
Why does the last printf show us the content of b? Isn't it supposed to show us the address of b but in %s format?
I thought printf("Print content of string2\t= %s\n", *string2); was the correct method of getting the content of b printed out through string2 but apparently it was the wrong way.
%sexpects a pointer to char, internally it dereferences it. If you pass*string2it will interpret the char value as a pointer and you will most likely hit a segmentation fault&b[0]is the memory address of the first char of thebarray.bis the memory address of thebarray which by definition is the memory address of the first element of thebarray. IOWbrepresents the same memory address than&b[0]. Or still IOW:char* string2 = &b[0]is the same thing aschar* string2 = b.