0

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

1
  • 1
    enable warning will help you... Commented Apr 8, 2018 at 10:36

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.