结构体数组指针做函数参数
时间: 2024-06-20 17:03:46 AIGC 浏览: 316
结构体数组指针作为函数参数时,可以方便地传递多个结构体的信息,并且在函数内对结构体数组进行操作。具体的介绍如下:
1. 结构体数组指针的定义方式如下:
```
struct Student {
char name[20];
int age;
float score;
};
void func(struct Student *stu, int n) {
// do something
}
```
2. 在调用函数时,需要传递结构体数组的地址作为参数:
```
struct Student stu[10];
func(stu, 10);
```
3. 在函数内,可以通过指针对结构体数组进行遍历和操作:
```
void func(struct Student *stu, int n) {
for (int i = 0; i < n; i++) {
printf("%s %d %.2f\n", stu[i].name, stu[i].age, stu[i].score);
}
}
```
4. 另外,也可以通过结构体指针的方式访问结构体数组中的元素:
```
void func(struct Student *stu, int n) {
for (int i = 0; i < n; i++) {
printf("%s %d %.2f\n", (stu + i)->name, (stu + i)->age, (stu + i)->score);
}
}
```
阅读全文
相关推荐










