结构体\结构体数组

2023-12-26 15:56:07
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//定义结构体
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;
}

文章来源:https://blog.csdn.net/2301_79018328/article/details/135221320
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。