数组名是首地址,那能用*加数组名表示首元素的值么?定义一个结构体数组st[3]后,能否用st->num这种格式合法么?
#include
#include
struct st{
char c;
int num;
};
int main(int argc,char ** argv)
{
struct st s[4] = {0};
//initial s[0]
s[0].c = 'a';
s[0].num = 99;
printf("1.The first st :c is %c,num is %d.\n",s->c,s->num);
//
printf("2.The first st :c is %c,num is %d.\n",s[0].c,s[0].num);
//
printf("3.The first st :c is %c,num is %d.\n",(&s[0])->c,(&s[0])->num);
//
printf("4.The first st :c is %c,num is %d.\n",(*s).c,(*s).num);
return 0;
}
输出:
1.The first st :c is a,num is 99.
2.The first st :c is a,num is 99.
3.The first st :c is a,num is 99.
4.The first st :c is a,num is 99.
其实,楼主已经知道 数组名是首地址就是很好理解了。首地址去内容就是s[0],s->num,就是第一个元素的指针指向它其中的元素。
能用*加数组名表示首元素的值
能用st->num这种格式
都是可以的
你可以用编译器自己测试一下