C语言结构体类型定义+结构体变量的定义与使用及其初始化+结构体变量作为函数参数

上一篇文章:返回指针值的函数+指向函数的指针+main()函数的参数

C语言结构体类型定义+结构体变量的定义与使用及其初始化+结构体变量作为函数参数

结构体

引例

输出平均分最高的学生信息

#include <stdio.h>
struct student
{
	int num;
	char name[10];
	int computer,english,math;
	double average;//这些都是结构体成员
};//注意这个分号不要可少,否则会报错
int main()
{
	int i,n;
	struct student s1,max;//定义结构体变量s1和结构体变量max
	printf("Input n:");
	scanf("%d",&n);//n是学生的个数
	printf("Input the student's number, name and course scores:\n");
	for(i=1;i<=n;i++)//用for循环实现n个学生成绩的输入
	{
		printf("No.%d:",i);//提示学生的序号
		scanf("%d%s%d%d%d",&s1.num,s1.name,&s1.math,&s1.english,&s1.computer);
		//分别给结构体里面的变量输入数值
		s1.average=(s1.math+s1.english+s1.computer)/3.0;//计算出该学生的平均成绩
		if(i==1)max=s1;
		if(max.average<s1.average)//找出平均分最高的学生
			max=s1;//将s1里面的成员变量的值分别对应地赋值给max
	}
	printf("num:%d, name:%s, average:%.2lf\n",max.num,max.name,max.average);
	//将平均成绩最高的学生成绩输出
	return 0;	
}

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 结构体类型的定义:
    结构体类型实际上是一种模板,它的定义形式为:
struct 结构体名
{
	类型标识符 结构体成员名1;
	类型标识符 结构体成员名2;
	...
	类型标识符 结构体成员名3;
};//最后的这个分号不要忘了

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

注意不要忘记最后的分号

结构体变量的定义

在结构体类型定义好的情况下,注意是结构体类型定义好的情况下,才能定义结构体变量。
比如:

struct student
{
	int num;
	char name[10];
	int computer,english,math;
	double average;
};

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

这个是结构体类型的定义,然后才能进行结构体变量的定义:

struct 结构体类型名 结构体变量名;

  
  • 1
struct student stu1,stu2;//定义两个结构体变量

  
  • 1

也可以在定义结构体类型的时候同时定义结构体变量,如:

struct student
{
	int num;
	char name[10];
	int computer,english,math;
	double average;
}stu1,stu2;//定义两个结构体变量;

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

也可以省略结构体类型名,如:

struct//省去了结构体类型的名字student
{
	int num;
	char name[10];
	int computer,english,math;
	double average;
}stu1,stu2;//定义两个结构体变量;

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

省略了结构体类型的名字,在这种情况下,结构体变量只能在后面同时定义,而不能在主函数中定义。所以并不建议这样定义结构体变量

  • 注意:结构体变量的变量名和成员名可以相同,二者互不影响,互不干扰,如:
struct date
{
	int year;
	int month;
	int day;
};
int main()
{
	struct date year;
	year.year = 1980;
}

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

结构体变量的使用

使用形式为:结构体变量.结构体成员名

scanf("%d%s%d%d%d",&s1.num,s1.name,&s1.math,&s1.english,&s1.computer);

  
  • 1
  • 注意:如果结构体类型中有字符数组成员,并且在主函数中要给字符数组赋字符串常量时,不能直接用“=”,即s1.name="张三";是错误的,必须使用字符串复制函数strcpy()函数来实现,如:strcpy(s1.name,"张三");
    在这里插入图片描述
    在这里插入图片描述
  • 同一类型的结构体变量间可以赋值
    如:stu2=stu1;将结构体变量stu1里面的所有成员变量的值分别对应赋给结构体变量stu2

结构体变量作为函数参数

  • 结构体变量的成员作为函数的实参形参为普通变量或数组
  • 也可以将结构体变量作为函数的参数
    例如:(请看代码中的详细注释,并观察运行结果)
#include <stdio.h>
#include <string.h>
struct s_score
{
	int no;
	char name[10];
	int score[3];
};//不要忘记这个分号
void output(struct s_score a);//形参为结构体变量
void fun(int *q);//形参为指针 
main()
{
	struct s_score a={1001,"zhangsan",{60,60,60}};//这行语句是对结构体变量的初始化
	output(a);
	a.no=1001;
	strcpy(a.name,"wang lin");
	a.score[0]=78;
	a.score[1]=88;
	a.score[2]=94;
	output(a);
	fun(a.score);//结构体中的数组成员,数组名为数组首地址 
	output(a);
}
void output(struct s_score a)//将结构体变量作为形参接收来自主函的结构体变量 
{
	int i;
	printf("%d  %s   ",a.no,a.name);
	for(i=0;i<3;i++)
		printf("%4d",a.score[i]);
	printf("\n");
}
void fun(int *p)//定义指针变量来接收主函数传入的数组首地址 
{
	int i;
	for(i=0;i<3;i++)
	{
		*(p+i)+=10;//通过循环变量i来对指针变量进行运算 
		if(*(p+i)>100)
			*(p+i)=100;
	}
}

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

结构体变量的初始化

结构体变量的初始化一般形式为:
结构体变量名={初值表};
就像上面的语句:

struct s_score a={1001,"zhangsan",{60,60,60}};

  
  • 1

结构体初值的数据类型,应与结构体变量中相应成员所要求的一致,否则会出错



posted @ 2022-11-21 18:54  TwcatL_tree  阅读(12)  评论(0编辑  收藏  举报