结构体变量的定义有四种方式:
(1)方式1
struct student{
...
};
struct student stu1,stu2;
注:在C++中用此种方式定义变量时,直接写student stu1,stu2即可,不用加前面的struct。
(2)方式2
struct student{
...
}stu1,stu2;
(3)方式3:定义无名结构体变量
struct {
...
}stu1,stu2;
(4)方式4
typedef struct{
...
}Stu;
Stu stu1,stu2;
结构体变量的初始化:
struct student{
char id[10];
char name[10];
int score[3];
int sum;
};
struct student stu1={"2013","张三",87,89,96};
对结构体变量的引用:
结构体变量名.成员名
结构体数组
struct student{
char id[10];
char name[10];
int score[3];
int sum;
};
struct student stu[10];
结构体数组作为函数参数
有两个参数:一个数组名,另一个是数组中元素个数
void sort(struct student a[],int n){...}
int main(){
...
sort(stu,10);
...
}