eternum
eternum

Reputation: 31

How to resolve "parameter has incomplete type" error?

I'm a newbie and I need help with debugging my code. When I compile it 'type of formal parameter 1 is incomplete' and type of formal parameter 2 is incomplete' error appears in

 printf("Age is %d years.\n", calc_age(birth, current));

while 'parameter 1 ('birth') has incomplete type' and ' parameter 2 ('current') has incomplete type' errors appear in

int calc_age (struct date birth, struct date current) {

Help is appreciated, thanks!

#include <stdio.h>
int calc_age (struct date birth, struct date current);


int main(void)
{

    struct date {
        int month[1];
        int day[1];
        int year[1];

};


    struct date birth, current;
    char c;

    printf("Input the birthdate (MM/DD/YY): ");
    scanf("%d %c %d %c %d", birth.month, &c, birth.day, &c, birth.year);
    printf("Input date to calculate (MM/DD/YY): ");
    scanf("%d %c %d %c %d", current.month, &c,  current.day, &c, current.year);

    printf("Age is %d years.\n", calc_age(birth, current));

    return 0;

}

int calc_age (struct date birth, struct date current) {

    int age;

    if (birth.month[0] < current.month[0]) {
        age = (current.year[0] - birth.year[0]);
    } else if (birth.month[0] > current.month[0]) {
        age = (current.year[0] - birth.year[0] - 1);
    } else {
        if (birth.day[0] <= current.day[0]) {
            age = (current.year[0] - birth.year[0]);
        } else {
            age = (current.year[0] - birth.year[0] - 1);
        }
    }

    return age;
}

Upvotes: 1

Views: 17147

Answers (2)

Sourav Ghosh
Sourav Ghosh

Reputation: 134396

Your program is showing incomplete type error because the scope of struct date is limited to main() function only. Outside the main() the structure definition is not visible.

So, the struct date definition should be in global scope so that it is visible from calc_age() (and maybe other functions, too). Even better, if you can create and maintain a header file for this purpose.

That said, in your code, as per your current requirement, get rid of single-element arrays in the structure, like

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

and also, the scanf() statement

scanf("%d %c %d %c %d", birth.month, &c, birth.day, &c, birth.year);

should read

scanf("%d %c %d %c %d", &birth.month, &c, &birth.day, &c, &birth.year);

Upvotes: 7

icecity96
icecity96

Reputation: 1227

#include <stdio.h>
struct date {
    int month[1];
    int day[1];
    int year[1];

};

int calc_age (struct date birth, struct date current);


int main(void)
{
struct date birth, current;
char c;

printf("Input the birthdate (MM/DD/YY): ");
scanf("%d %c %d %c %d", birth.month, &c, birth.day, &c, birth.year);
printf("Input date to calculate (MM/DD/YY): ");
scanf("%d %c %d %c %d", current.month, &c,  current.day, &c, current.year);

printf("Age is %d years.\n", calc_age(birth, current));

return 0;

}

int calc_age (struct date birth, struct date current) {
int age;

if (birth.month[0] < current.month[0]) {
    age = (current.year[0] - birth.year[0]);
} else if (birth.month[0] > current.month[0]) {
    age = (current.year[0] - birth.year[0] - 1);
} else {
    if (birth.day[0] <= current.day[0]) {
        age = (current.year[0] - birth.year[0]);
    } else {
        age = (current.year[0] - birth.year[0] - 1);
    }
}

return age;
}

You should define the struct before main

Upvotes: 4

Related Questions