久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      C 語(yǔ)言關(guān)于結(jié)構(gòu)體做參數(shù)傳遞

      首先結(jié)構(gòu)體做函數(shù)參數(shù)有三種傳遞方式:

      一是傳遞結(jié)構(gòu)體變量,這是值傳遞,二是傳遞結(jié)構(gòu)體指針,這是地址傳遞,三是傳遞結(jié)構(gòu)體成員,當(dāng)然這也分為值傳遞和地址傳遞。

      以傳引用調(diào)用方式傳遞結(jié)構(gòu)比用傳值方式傳遞結(jié)構(gòu)效率高。以傳值方式傳遞結(jié)構(gòu)需要對(duì)整個(gè)結(jié)構(gòu)做一份拷貝。

      下面看一個(gè)列子,student結(jié)構(gòu)體中包含該學(xué)生的各種信息,我們?cè)赾hange函數(shù)中對(duì)其進(jìn)行部分修改,再在主函數(shù)中輸出其結(jié)果

      1.下面?zhèn)鬟f結(jié)構(gòu)體變量

      #include<stdio.h>  #include<string.h>  #define format "%dn%sn%fn%fn%fn"  struct student  {      int num;      char name[20];      float score[3];  };  void change( struct student stu );  int main()  {            struct student stu;      stu.num = 12345;      strcpy(stu.name, "Tom");      stu.score[0] = 67.5;      stu.score[1] = 89;      stu.score[2] = 78.6;      change(stu);      printf(format, stu.num, stu.name, stu.score[0], stu.score[1],stu.score[2]);      printf("n");      return 0;  }     void change(struct student stu)  {      stu.score[0] = 100;      strcpy(stu.name, "jerry");  }

      C 語(yǔ)言關(guān)于結(jié)構(gòu)體做參數(shù)傳遞

      可以看到最終輸出的值并未改變。。。

      2.地址傳遞

      #include<stdio.h>  #define format "%dn%sn%fn%fn%fn"  struct student  {      int num;      char name[20];      float score[3];  };  void change( struct student* stu );  int main()  {            struct student stu;      stu.num = 12345;      strcpy(stu.name, "Tom");      stu.score[0] = 67.5;      stu.score[1] = 89;      stu.score[2] = 78.6;      change(&stu);      printf(format, stu.num, stu.name, stu.score[0], stu.score[1],stu.score[2]);      printf("n");      return 0;  }     void change(struct student* p)  {      p->score[0] = 100;      strcpy(p->name, "jerry");  }

      C 語(yǔ)言關(guān)于結(jié)構(gòu)體做參數(shù)傳遞

      可以看到,通過(guò)地址傳遞修改了結(jié)構(gòu)體內(nèi)的數(shù)據(jù)

      用&stu做實(shí)參,&stu是結(jié)構(gòu)體變量stu的地址。在調(diào)用函數(shù)時(shí)將該地址傳送給形參p(p是指針變量)。這樣p就指向stu。

      在change函數(shù)中改變結(jié)構(gòu)體內(nèi)成員的值,在主函數(shù)中就輸出了改變后的值

      3.結(jié)構(gòu)體成員的地址傳遞和值傳遞

      這個(gè)類似于單一變量的傳遞,這里也沒(méi)必要說(shuō)了,當(dāng)然是地址傳遞才能修改。

      把一個(gè)完整的結(jié)構(gòu)體變量作為參數(shù)傳遞,要將全部成員值一個(gè)一個(gè)傳遞,費(fèi)時(shí)間又費(fèi)空間,開(kāi)銷大。如果結(jié)構(gòu)體類型中的成員很多,或有一些成員是數(shù)組,則程序運(yùn)行效率會(huì)大大降低。在這種情況下,用指針做函數(shù)參數(shù)比較好,能提高運(yùn)行效率。

      原文地址:https://blog.csdn.net/lin37985/article/details/38582027

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)