![](/img/trans.png)
[英]passing an argument ( a struct in an array of structs within a struct) to a function in C
[英]passing a struct array as argument to function in C
我試圖將其打印出來,但是它一直失敗,並且僅打印地址,我對C還是陌生的,不太確定如何解決此問題。
我有兩個結構和兩個方法,
struct Date {
char week_day[30];
int day[31];
int month[12];
};
struct Holiday {
char name[80]; //name
struct Date date; //date
};
void printHols(struct Holiday hol[]){
printf("Holidays in 2018\n");
for (int i=0; i<2; i++) {
printf("%d / %d \t - %s \t - %s", hol[i].date.day, hol[i].date.month, hol[i].date.week_day, hol[i].name);
}
}
void holidaysValues(){
struct Holiday holiday={{"New Year",{"Monday",1,1}}, {"Some Holiday",{"Tuesday",2,3}} };
//passing this struct below doesn't work as expected, prints addresses of how[I].date.day, hol[I].date.month
printHols(&holiday);
}
歡迎所有建議。 謝謝
我已經修正了您的代碼。
首先,我確定您打算將int用於日期和月份而不是它們的數組。 而且您忘記為假日添加[]。 在您完成之后-無需在printHols(&holiday);中引用假日
我還添加了\\ n到printf,但這只是為了獲得更好的輸出。
#include <stdio.h>
struct Date {
char week_day[30];
int day;
int month;
};
struct Holiday {
char name[80]; //name
struct Date date; //date
};
void printHols(struct Holiday hol[]){
printf("Holidays in 2018\n");
for (int i=0; i<2; i++) {
printf("%d / %d \t - %s \t - %s \n", hol[i].date.day, hol[i].date.month, hol[i].date.week_day, hol[i].name);
}
}
void main(){
struct Holiday holiday[] = {{"New Year",{"Monday",1,1}}, {"Some Holiday",{"Tuesday",2,3}} };
printHols(holiday);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.