c语言结构体类型的参数传递
时间: 2023-11-23 17:57:02 浏览: 37
C语言中,结构体类型的参数传递可以通过值传递或指针传递来实现。当结构体较小时,可以使用值传递,而当结构体较大时,使用指针传递可以提高程序的效率。
以下是两种不同的结构体参数传递方式的示例:
1. 值传递方式
```c
#include <stdio.h>
#include <string.h>
struct Student {
char name[20];
int age;
};
void printStudent(struct Student s) {
printf("Name: %s\n", s.name);
printf("Age: %d\n", s.age);
}
int main() {
struct Student s1 = {"Tom", 18};
printStudent(s1);
return 0;
}
```
输出结果为:
```
Name: Tom
Age: 18
```
2. 指针传递方式
```c
#include <stdio.h>
#include <string.h>
struct Student {
char name[20];
int age;
};
void printStudent(struct Student *s) {
printf("Name: %s\n", s->name);
printf("Age: %d\n", s->age);
}
int main() {
struct Student s1 = {"Tom", 18};
printStudent(&s1);
return 0;
}
```
输出结果与上面相同。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)