# 一.结构体变量的定义和初始化

``````typedef struct
{
char name[20];
int age;
char sex[5];
int id;
}Stu;//命名一个新的类型名代表结构体类型
int main()
{
Stu Z = { "zhangsan",18,"mate",12345};//直接用Stu代替struct

struct s//类型声明
{
char name[20];
int age[10];
int score[10];
};
struct member
{
char name[20];
char sex[10];
}p1,p2;//声明类型的同时，定义p1，p2变量，这里的p1,p2是全局变量
struct member p3;//定义结构体变量p3
struct s t1 = {"zhangsan", 18 , 95 };//初始化

return 0;
}``````

1.在声明类型的同时定义变量，这时定义的变量为全局变量

2.可以单独分开定义结构体变量

``````#include <stdio.h>
struct Stu
{
char name[20];
int age[5];
int score;
};

struct s
{
struct Stu Z;
int height;
char sex[10];
float weight;
}p1 = { {"zhangsan",18,95 },175,"mate",70.5};//结构体嵌套初始化

int main()
{
struct s p2 = { {"lisi",19,85},180,"mate",80.2 };//结构体嵌套初始化，局部变量
return 0;
}``````

# 二.结构体传参

``````struct Stu
{
int arr[10];
char name[20];
int age;
int score;
}p1;

void print1(struct Stu Z)//结构体传参
{
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", Z.arr[i]);
}
printf("n");
printf("%s ", Z.name);
printf("%d ", Z.age);
printf("%d ", Z.score);
printf("n");
}

void print2(struct Stu* Z)//结构体地址传参
{
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", Z->arr[i]);
}
printf("n");
printf("%s ", Z->name);
printf("%d ", Z->age);
printf("%d ", Z->score);
printf("n");
}

#include <stdio.h>
int main()
{
struct Stu Z = { {1,2,3,4,5,6,7,8,9,10},"zhangsan",18,95 };
print1(Z);//传结构体(传值)
printf(&Z);//传地址（传址）
return 0;
}``````

THE END