Reputation: 592
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.
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
Upvotes: 0
Views: 61
Reputation: 150
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. 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);
I've also added \n to printf but it's just for a better output.
#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);
}
Upvotes: 2