When should an array name be treated as a pointer and when does it just represent the array itself? [duplicate]
- by user1087373
This question already has an answer here:
When is an array name or a function name 'converted' into a pointer ? (in C)
4 answers
I just made a test program after reading the book and the result turned out confusing:
#include <stdio.h>
int main(void)
{
char text[] = "hello!";
printf("sizeof(text):%d sizeof(text+2):%d sizeof(text[0]):%d \n",(int)sizeof(text), sizeof(text+2), sizeof(text[0]));
printf("text:%p sizeof(text):%d &text:%p sizeof(&text):%d \n",text, sizeof(text), &text, sizeof(&text));
printf("text+1:%p &text+1:%p \n", text+1, &text+1);
return 0;
}
The result:
sizeof(text):7 sizeof(text+2):4 sizeof(text[0]):1
text:0xbfc8769d sizeof(text):7 &text:0xbfc8769d sizeof(&text):4
text+1:0xbfc8769e &text+1:0xbfc876a4
What makes me feel confused are:
why the value of 'sizeof(text)' is 7 whereas 'sizeof(text+2)' is 4
what's the difference between 'text' and '&text'?