0

I have a problem with calling structs, putting them as argument in a void function, and then call another void function, with argument taking the same struct, so I put them both pointers and the result is not showing up. If I call them separately, it works, but with the nested functions, it's not working.

#include <stdio.h>
#include <stdlib.h>

typedef struct date {
        int day;
        int month;
        int year;
} date;

void next_day(date *d,int days)
{
        if(d->day < days) {
                int m = d->day + 1;
                d->day = m;
        }
        else
        {
                int m = d->year + 1;
                d->year = m;
                d->month = 1;
        }
}

void tom(date *d)
{
        switch(d->month)
        {
                case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                        next_day(&d,31);
                        break;
                case 4: case 6: case 9: case 11:
                        next_day(&d,30);
                        break;
                case 2:
                        if(d->year % 4 == 0)
                                next_day(&d,29);
                        else
                                next_day(&d,28);
                        break;
        }
}

int main(void)
{
        //date d = malloc(sizeof(date));
        date d;
        printf("Enter day, month, year: ");
        scanf("%d %d %d",&(d.day),&(d.month),&(d.year));
        tom(&d);
        printf("The next day is: %d %d %d",d.day,d.month,d.year);
        return 0;
}
1
  • What do you mean by the result is not showing up? Commented Nov 27, 2011 at 19:50

2 Answers 2

1
next_day(&d,31);   // At this point `d` is a pointer to a struct type. 

You are sending the address of pointer, which means at the receiving end the function argument needs to be a pointer to a pointer ( i.e., date ** ). But in your case, it is not.

void next_day(date *d,int days)

So, just do -

next_day(d,31);  // Notice the removal of & 

Now d is of type date * and the receiving end also the argument is of type date *.

Sign up to request clarification or add additional context in comments.

Comments

1

I am fairly sure your problem is with next_day(&d,31); You already have the address of the structure, you do not need to use the & operator again. Try calling it with next_day(d,31);.

This should have shown up in the compiler warning; always take care to read those warning.

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.