C语言 结构体作为函数的参数
生活随笔
收集整理的這篇文章主要介紹了
C语言 结构体作为函数的参数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1)使用結構體變量作為函數的參數
使用結構體變量作為函數的實參時,采用的是值傳遞,會將結構體變量所占內存單元的內容全部順序傳遞給形參,形參必須是同類型的結構體變量
demo:
1 # include <stdio.h>
2 # include <stdlib.h>
3
4 //創建一個Student結構
5 struct Student
6 {
7 char name[30];
8 float fScore[3];
9 }student={"dire",98.5,89.0,93.5}; //初始化結構體變量
10
11 void Display(struct Student su) //形參為同類型的結構體(Student結構)
12 {
13 printf("-----Information------
");
14 printf("Name:%s",su.name);
15 printf("Chinese:%.2f
",su.fScore[0]);
16 printf("Math:%.2f
",su.fScore[1]);
17 printf("English:%.2f",su.fScore[2]);
18 printf("平均分數為:%.2f
",(su.fScore[0]+su.fScore[1],su.fScore[2])/3);
19 }
20
21 int main ()
22 {
23
24 Display(student);
25
26 return 0;
27 }
Printf:
2)使用指向結構體變量的指針作為函數參數
Demo:
1 # include <stdio.h>
2 # include <stdlib.h>
3
4 struct Student {
5 char name[20];
6 float fScore[3];
7 }student = {"dire",98.5,89.0,93.5}; //初始化結構體變量
8
9
10 void Display(struct Student *pStruct)
11 {
12 printf("------Information-------
");
13 printf("Name:%s
",pStruct->name);
14 printf("Chinese:%.2f
",(*pStruct).fScore[0]);
15 printf("Math:%.2f
",(*pStruct).fScore[1]);
16 printf("English:%.2f
",pStruct->fScore[2]);
17 }
18
19
20 int main ()
21 {
22 Display(&student); //將結構體變量的首地址作為實參傳入pStruct指針變量中
23
24 return 0;
25 }
3)使用結構體變量的成員作為函數參數
這種方式為函數傳遞參數與普通的變量作為實參是一樣的,是值傳遞
總結
以上是生活随笔為你收集整理的C语言 结构体作为函数的参数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机C语言常用语句,计算机二级C语言考
- 下一篇: 借助百度云API进行人脸识别