customer.name 是指向name 字符数组中第一个字符的指针。
customer.name 等价于&(customer.name[0])
customer.name 约定是访问结构中字符数组的典型方式。
在printf() %s 格式中需要指向数组第一个元素的指针。
结构体a、成员name数组和name[0]字符在内存中的地址完全相同。您可以在以下程序中看到:
#include <stdio.h>
#include <stdlib.h>
typedef struct a {
char name[18];
}customer;
int main()
{
customer bob;
customer *ptr;
scanf("%s",bob.name);
printf("Customer name is: %s\n",bob.name);
ptr = & bob;
printf("Memory Address of the bob structure is: %p\n", (void *) ptr);
printf("Memory Address of the 'bob.name' is: %p\n", (void *) &bob.name);
printf("Memory Address of the 'bob.name[0]' is: %p\n", (void *) &(bob.name[0]) );
return 0;
}
输出:
Bob
Customer name is: Bob
Memory Address of the bob structure is: 0x7ffe0d7687e0
Memory Address of the 'bob.name' is: 0x7ffe0d7687e0
Memory Address of the 'bob.name[0]' is: 0x7ffe0d7687e0