一、定义
注意:
![](https://img-blog.csdnimg.cn/26779fefbdc04abd8ca102d45ffdc659.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA4oSz4LmT5rW35bG_5pif6L6wIOC4heC4hQ==,size_14,color_FFFFFF,t_70,g_se,x_16)
struct student //student是结构体名
{
int num;
int score;
float avg;
}
二、结构体变量的定义
struct student //student是结构体名
{
int num;
int score;
float avg;
};
struct student //student是结构体名
{
int num;
int score;
float avg;
};
int main(void)
{
int a; //a占据4个字节
//变量的定义:类型名 变量名
struct student Tom={10,90,85}; //Tom 是变量名
Tom.num=12;
Tom.score=87;
Tom.avg=78;
printf("序号:%d\n",Tom.num);
printf("成绩:%d\n", Tom.score);
printf("平均分:%f\n", Tom.avg);
return 0;
}
struct student //student是结构体名
{
int num;
int score;
float avg;
}Jerry,Jim; //}Jerry是结构体变量
struct //student是结构体名
{
int num;
int score;
float avg;
}Jerry,Jim; //}Jerry是结构体变量
//定义结构体包含学生的序号、3门课的成绩、平均分
struct student //student是结构体名
{
int num;
int score[3];
float avg;
}Jerry,Jim; //}Jerry是结构体变量int main(void)
{
int a; //a占据4个字节
//变量的定义:类型名 变量名
struct student Tom={10,{70.88,90},78}; //Tom 是变量名
struct student Jim={10,70,88,90.5};
printf("序号:%d\n",Jim.num);
printf("成绩1:%d\n", Jim.score[0]);
printf("成绩2:%d\n", Jim.score[1]);
printf("成绩3:%d\n", Jim.score[2]);
printf("平均分:%f\n", Jim.avg);
return 0;
}
三、typedef的使用
typedef struct student //student是结构体名
{
int num;
int score[3];
float avg;
}STU; //STU是结构体struct student的别名struct stu //student是结构体名
{
int num;
int score[3];
float avg;
}Tom; //Tom是结构体struct stu的变量
四、结构体数组