C语言之结构体新写法
作者:互联网
#include <stdio.h>
struct Student {
char *name;
int ival;
char *chval;
};
struct Student stu[] = {
{
.name = "JIM",
.ival = 5,
.chval = "ARM",
},
{
.name = "GReen",
.ival = 6,
.chval = "X86",
},
};
int main(){
//Way:1
printf("name = %s, ival = %d, chval = %s\n",stu[0].name,stu[0].ival,stu[0].chval);
printf("name = %s, ival = %d, chval = %s\n",stu[1].name,stu[1].ival,stu[1].chval);
//结构体元素个数
printf("count = %ld\n",sizeof(stu) / sizeof(struct Student));
//Way:2
int i;
for(i = 0; i < (sizeof(stu) / sizeof(struct Student)); i++){
printf("name = %s, ival = %d, chval = %s\n",stu[i].name,stu[i].ival,stu[i].chval);
}
}
标签:name,体新,C语言,stu,chval,printf,ival,写法,sizeof 来源: https://blog.csdn.net/u010164190/article/details/123063569