# 【C语言】结构体指针与结构体数组

C语言 专栏收录该内容
27 篇文章 0 订阅

1.结构体数组的定义

2.结构体数组的初始化

3.结构体数组的引用

4.结构体数组指针

## 一、结构体指针

struct Student stu = { 1001,"Li Lei",'M',1.87 };  //定义一个Student 的结构体变量stu

struct Student *p=&stu;    //定义一个Student的结构体指针变量来指向stu

#include <stdio.h>

typedef char NAME[10];
typedef double DOU;
typedef struct Student
{
int num;
NAME name;
char sex;
DOU height;

}Stu;

int main() {
Stu stu = { 1001,"Li Lei",'M',1.87 };
Stu* p = &stu;
printf("学生信息：\n");
printf("学号：%d\n", p->num);
printf("姓名：%s\n", p->name);
printf("性别：%c\n", p->sex);
printf("年龄：%.2lf\n", p->height);
}

## 二、结构体数组

### 1.结构体数组的定义

①先定义结构体类型，后定义结构体数组

struct Student
{
int num;
char name[10];
char sex;
double height;

};

struct Student stus[20];

②在定义结构体类型的同时定义结构体数组

struct Student
{
int num;
char name[10];
char sex;
double height;

}stus[20];

③直接定义结构体数组

struct
{
int num;
char name[10];
char sex;
double height;

}stus[20];

### 2.结构体数组的初始化

①先定义结构体类型，后定义并初始化结构体数组

struct Student
{
int num;
char name[10];
char sex;
double height;

};

struct Student stu1[3] = { {1001,"Li Lei",'M',1.78},
{1002,"Li Hua",'M',1.87},
{1003,"Han Mei",'W',1.65}
};

②在定义结构体类型的同时定义结构体数组并初始化

struct Student
{
int num;
char name[10];
char sex;
double height;

}stu1[3] = { {1001,"Li Lei",'M',1.78},
{1002,"Li Hua",'M',1.87},
{1003,"Han Mei",'W',1.65}
};



### 3.结构体数组的引用

#include <stdio.h>

struct Student
{
int num;
char name[10];
char sex;
double height;

};

struct Student stus[3] = { {1001,"Li Lei",'M',1.78},
{1002,"Li Hua",'M',1.87},
{1003,"Han Mei",'W',1.65}

};

int main() {
for (int i = 0; i < 3; i++) {
printf("学生%d的信息\n", i + 1);
printf("学号：%d\n", stus[i].num);
printf("姓名：%s\n", stus[i].name);
printf("性别：%c\n", stus[i].sex);
printf("身高：%.2lf\n", stus[i].height);
printf("\n");
}

}


### 4.结构体数组指针

#include <stdio.h>

struct Student
{
int num;
char name[10];
char sex;
double height;

};

struct Student stus[3] = { {1001,"Li Lei",'M',1.78},
{1002,"Li Hua",'M',1.87},
{1003,"Han Mei",'W',1.65}

};

int main() {
struct Student* p=stus ;//定义结构体数组指针p指向结构体数组stus

for (int i = 0; i < 3; i++) {
printf("学生%d的信息\n", i + 1);
printf("学号：%d\n", p->num);
printf("姓名：%s\n", p->name);
printf("性别：%c\n", p->sex);
printf("身高：%.2lf\n", p->height);
printf("\n");
p++;
}

}

• 4
点赞
• 1
评论
• 4
收藏
• 一键三连
• 扫一扫，分享海报

02-15
08-16 2万+

07-31 2万+
01-23 7524
12-10 654
06-15 4766
03-02 4239
11-10 312
05-31 6687
02-22 2005
03-14 634
04-15
06-29 1415
12-28

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。