您可以使用struct date
變量,初始化值,這樣做:
#include <stdio.h>
struct date{
int month;
int day;
int year;
};
struct employee{
struct date datehired;
};
int main(void)
{
struct employee e[3];
struct date initVal= {2,2,16};
for(size_t i=0;i<sizeof(e)/sizeof(e[0]);i++){
e[i].datehired=initVal;
}
}
或者使用Compound Literals
#include <stdio.h>
struct date{
int month;
int day;
int year;
};
struct employee{
struct date datehired;
};
int main(void)
{
struct employee e[3];
for(size_t i=0;i<sizeof(e)/sizeof(e[0]);i++){
e[i].datehired=(struct date){2,2,16};;
}
}
而且最簡單和基本的代碼
#include <stdio.h>
struct date{
int month;
int day;
int year;
};
struct employee{
struct date datehired;
};
int main(void)
{
struct employee e[3];
for(size_t i=0;i<sizeof(e)/sizeof(e[0]);i++)
{
e[i].datehired.month = 2;
e[i].datehired.day = 2;
e[i].datehired.year = 16;
}
}
來源
2017-03-17 13:39:29
LPs
感謝解決我的問題是一個偉大而短暫的解決方案 –