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);
	}
}

上一篇:P2858 [USACO06FEB]奶牛零食Treats for the Cows


下一篇:python入门学习篇二十一