當前位置:首頁 » 編程語言 » c語言結構體嵌套

c語言結構體嵌套

發布時間: 2023-06-15 14:15:34

㈠ C語言 如何在結構體中給嵌套的結構體賦值

結構體嵌套,賦初值,大括弧嵌套就可以了。
問題是你代碼寫錯了。
你定義的old1不是結構類型,而是結構變數。
在shuju中成員要寫struct old 變數名;
不能寫struct old1。
下面是演示代碼:(補充:如果想定義一個結構類型別名,要用typedef

#include <stdio.h>

struct old

{

int year;

int month;

int day;

}old1={1972, 8, 3};

struct shuju

{

char name[10];

char gender[6];

int height;

struct old old1;

char access[9];

}shuju1={"moumouren","male",175,{2019,6,29},"well"};

int main()

{

printf("結構變數old1的初值: %d %d %d ",old1.year,old1.month,old1.day);

printf("結構變數shuju1中成員old1的初值:%d %d %d ",shuju1.old1.year,shuju1.old1.month,shuju1.old1.day);

printf("我特意起一樣的名字,就是告訴你這兩個變數是不一樣的。 ");

return 0;

}

/*(補充:如果想定義一個結構類型別名,要用

typedef struct old

{

int year;

int month;

int day;

}old1;//這樣old1才表示是一個類型,然後再定義該類型變數時,才可以寫 old1 變數名;

)*/

熱點內容
ftp匿名帳號 發布:2025-02-12 18:04:32 瀏覽:763
銳志哪個配置性價比最高 發布:2025-02-12 17:38:43 瀏覽:918
智能推送演算法 發布:2025-02-12 17:38:41 瀏覽:835
拍照上傳器 發布:2025-02-12 17:34:29 瀏覽:652
androidweb框架 發布:2025-02-12 17:32:45 瀏覽:76
安卓編程賀卡 發布:2025-02-12 17:32:44 瀏覽:838
php獲取資料庫的欄位 發布:2025-02-12 17:29:02 瀏覽:766
伺服器地址消失 發布:2025-02-12 17:23:36 瀏覽:951
後台執行php腳本 發布:2025-02-12 17:21:45 瀏覽:471
spring編程式事務 發布:2025-02-12 17:16:55 瀏覽:398