If you try this:
#include<stdio.h>
void main()
{
char name[]="siva";
printf("name = %p
", name);
printf("&name[0] = %p
", &name[0]);
printf("name printed as %%s is %s
",name);
printf("*name = %c
",*name);
printf("name[0] = %c
", name[0]);
}
Output is:
name = 0xbff5391b
&name[0] = 0xbff5391b
name printed as %s is siva
*name = s
name[0] = s
So 'name' is actually a pointer to the array of characters in memory. If you try reading the first four bytes at 0xbff5391b, you will see 's', 'i', 'v' and 'a'
Location Data
========= ======
0xbff5391b 0x73 's' ---> name[0]
0xbff5391c 0x69 'i' ---> name[1]
0xbff5391d 0x76 'v' ---> name[2]
0xbff5391e 0x61 'a' ---> name[3]
0xbff5391f 0x00 '' ---> This is the NULL termination of the string
To print a character you need to pass the value of the character to printf. The value can be referenced as name[0] or *name (since for an array name = &name[0]).
To print a string you need to pass a pointer to the string to printf (in this case 'name' or '&name[0]').
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…