结构体\结构体数组

#include
#include
#include

//定义结构体
typedef struct Date
{
	int year;
	int month;
	int day;
}Date;

typedef struct
{
	int id;
	char name[20];
	Date birthday;
	char sex;
	double score;
}Student;

int main()
{
	//定义结构体数组并初始化
	Student st[3] = { {1001,"JACK",{1992,5,21},'F',83},{1002,"LUCY",{1993,6,10},'M',66} };
	int i;

	//给最后一组数据赋值——原来的初值默认为零
	st[2].id = 1003;
	strcpy(st[2].name, "zyq");
	st[2].birthday.year = 2005;
	st[2].birthday.month = 6;
	st[2].birthday.day = 10;
	st[2].sex = 'M';
	st[2].score = 100;

	//用第二、三种方法访问结构体成员
	for (i = 0; i < 3; i++)
		printf("%d,%s, %d.%d.%d ,%c,%.2f\n", (st + i)->id, (st + i)->name, (st + i)->birthday.year, (*(st + i)).birthday.month, (st + i)->birthday.day, (st + i)->sex, (st + i)->score);

	system("pause");
	return 0;
}

你可能感兴趣的:(C语言进阶,c语言)