结构体代码组

结构体变量初始化

#include<string.h>
#include<stdio.h>
struct Student
{
	int id;
	char name[20];
	char gender;
	char class[40];
	char address[200];
}stu1 = { 1001, "C C++",'M',"LDCI0701","New York" };//定义时赋值
int main()
{
	struct Student stu2 = { 1002,"Ha H@",'F',"LDCI0702","CHINA" };
	printf("Student sut1:%d, ", stu1.id);
	printf("%s, ", stu1.name);
	printf("%c, ", stu1.gender);
	printf("%s, ", stu1.class);
	printf("%s\n", stu1.address);
	printf("Student sut2:%d, ", stu1.id);
	printf("%s, ", stu2.name);
	printf("%c, ", stu2.gender);
	printf("%s, ", stu2.class);
	printf("%s\n", stu2.address);
	return 0;
}
  • 演示
    在这里插入图片描述

结构体数组元素引用实例

#include<stdio.h>
#pragma warning(disable:4996);
struct STU
{
	char name[20];
	int age;
	char sex;
	char num[20];
};
void OutputSTU(struct STU stu[5]);
int main(void)
{
	int i;
	struct STU stu[5];
	for (i = 0; i < 5; ++i)
	{
		printf("请输入第%d个学生的信息:", i + 1);
		scanf("%s%d %c%s", stu[i].name, &stu[i].age, &stu[i].sex, stu[i].num);
	}
	OutputSTU(stu);
	return 0;
}
void OutputSTU(struct STU stu[5])
{
	struct STU stumax = stu[0];
	int j;
	for (j = 1; j < 5; j++)
	{
		if (strcmp(stumax.num, stu[j].num) < 0)
		{
			stumax = stu[j];
		}
	}
	printf("学生姓名:%s 学生年龄:%d 学生性别:%c 学生学号:%s\n", stumax.name, stumax.age, stumax.sex, stumax.num);

}
  • 演示
  • 学号最大的通过 outputSTU函数输出
    在这里插入图片描述

用结构体指针输出结构体数组的各项元素成员

#include<stdio.h>
#pragma warning(disable:4996);
struct stu
{
	int num;
	char* name;
	char sex;
	float score;
} *ps, boy[5] =
{
	{101,"Wang yi", 'M', 45},
	{102,"Wang er", 'F', 35},
	{103,"Wang sa", 'M', 55.5},
	{104,"Wang si", 'F', 65.5},
	{105,"Wang wu", 'M', 75},
};
int main()
{
	printf("No\tName\t\tSex\tScore\t\n");
	for (ps = boy; ps < boy + 5; ps++)
		printf("%d \t%s \t%c \t%f \t\n", ps->num, ps->name, ps->sex, ps->score);
	return 0;
}

  • 演示
    在这里插入图片描述

用结构体变量作为参数传递的示例

#include<stdio.h>
#define FORMAT "%d\n%s\n%f\n%f\n%f\n"
struct Student
{
	int num;
	char cName[20];
	float fScore[3];
};
int main()
{
	void print(struct Student); //函数声明, 形参类型为结构体Student
	struct Student stu; //定义结构体变量
	stu.num = 1001; // 以下5行对结构体变量各成员赋值
	strcpy(stu.cName, "JiaZhiJie");
	stu.fScore[0] = 77.5;
	stu.fScore[1] = 88;
	stu.fScore[2] = 98.5;
	print(stu);// 调用print函数,输出stu各成员的值
	return 0;
}
void print(struct Student stu)
{
	printf(FORMAT, stu.num, stu.cName, stu.fScore[0], stu.fScore[1], stu.fScore[2]);
}
  • 演示
    在这里插入图片描述

用结构体变量的成员作为参数传递的示例

#include<stdio.h>
struct Student
{
	int num;
	char cName[20];
	float fScore[3];
};
void print(char* cName)
{
	printf("cName: %s\n", cName);
}
int main()
{
	struct Student STU = { 1001,"ZhiZhen",88.5,59,77 };
	print(STU.cName);
	return 0;
}
  • 演示
    在这里插入图片描述

用指向结构体变量的指针作为参数传递示例

#include<stdio.h>
#define FORMAT "%d\t%s\t%f\t%f\t%f\n"
struct Student
{
	int num;
	char cName[20];
	float fScore[3];
}stu = { 1001, "ZhiZhen ",55.5,66.5,99}; //定义结构体student变量stu并赋值
int main()
{
	void print(struct Student*);//函数声明 形参为指向Student类型数据的指针变量
	struct Student* pt = &stu; //定义基类型为 Student的指针变量pt 并指向stu
	print(pt); //实参为指向Student类数据的指针变量
	return 0;
}
//定义函数 形参p是基类型为Student的指针变量
void print(struct Student* p)
{
	printf(FORMAT,p->num,p->cName,p->fScore[0],p->fScore[1],p->fScore[2]);
}

  • 演示
    在这里插入图片描述

用结构体数组元素的成员作为参数传递示例

#include<stdio.h>
struct Student
{
	int num;
	char name[20];
	char sex;
	int age;
	float score;
};
int main()
{
	void print(int num);
	struct Student stu[3] = { {1001,"ZhiZhen",'M',39,47},
											{1002,"WangAnShi",'M',40,99},
											{1003,"LiBai",'M',41,87} };
	print(stu[0].num);
}
void print(int num)
{
	printf("num: %d\n", num);
}
  • 演示
    在这里插入图片描述

用指向结构体数组的指针作为参数传递示例

#include<stdio.h>
#define FORMAT "%d\t%s\t%c\t%d\t%f\n"
struct Student
{
	int num;
	char name[20];
	char sex;
	int age;
	float score;
};
int main()
{
	void print(struct Student* p);
	struct Student stu[3] = { {1001,"ZhiZhen", 'M',10,47},
											{1002,"WangAn", 'W',22,99},
											{1003,"LiBai", 'M',11,87} };
	struct Student* pt = stu;
	print(pt);
}
void print(struct Student* p)
{
	for (int i = 0; i < 3; i++)
		printf(FORMAT, p[i].num, p[i].name, p[i].sex, p[i].age, p[i].score);
}
  • 演示
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值