[英]passing a struct array as argument to function in C
I am trying to print this out but it keeps failing, and prints the just the address, I"m new to C and not quite sure how to fix this. 我试图将其打印出来,但是它一直失败,并且仅打印地址,我对C还是陌生的,不太确定如何解决此问题。
I have two struct and two methods, 我有两个结构和两个方法,
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);
}
All suggestions are welcome. 欢迎所有建议。 Thanks 谢谢
I've fixed your code a bit. 我已经修正了您的代码。
First of all, I'm sure you meant to use ints for day and month not arrays of them. 首先,我确定您打算将int用于日期和月份而不是它们的数组。 And you forgot to add [] to holiday. 而且您忘记为假日添加[]。 And after you'll do it - there's no need to have a reference of holiday in printHols(&holiday); 在您完成之后-无需在printHols(&holiday);中引用假日
I've also added \\n to printf but it's just for a better output. 我还添加了\\ 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.