struct Student
{
int num;
char name[32];
char sex;
int age;
double score;
char addr[32];
}student1,student2;
重点认知:结构体没什么特殊的,只是把变量藏在结构体里面,而内部的变量,以前学习的东西是通用的,只是“触达的方式”不同
结构体可以这样定义但是不推荐。
#include <string.h>
#include <stdio.h>
struct Student
{
int num;
char name[32];
char sex;
int age;
double score;
char addr[32];
};
int main()
{
int a;
struct Student stu1;
struct Student stu2; // 定义两个结构体变量
a =10;
stu1.num = 1; // 点运算符还访问结构体的成员变量(域)
stu1.age = 10;
stu1.score = 98.6;
strcpy(stu1.name,"chenlicheng");
strcpy(stu1.addr,"山东");
printf("学号:%d 年龄 %d 分数 %.1f 姓名 %s 地址 %s",
stu1.num,stu1.age,stu1.score,stu1.name,stu1.addr);
return 0;
}
最好是在主函数中这样定义
struct Student stu1;
struct Student stu2;
点运算符还访问结构体的成员变量(域)
stu1.num = 1; // 点运算符还访问结构体的成员变量(域)
stu1.age = 10;
stu1.score = 98.6;
strcpy(stu1.name,"chenlicheng");
strcpy(stu1.addr,"山东");