日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

C语言 结构体作为函数的参数

發布時間:2023/12/15 综合教程 30 生活家
生活随笔 收集整理的這篇文章主要介紹了 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语言 结构体作为函数的参数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。