简体   繁体   中英

Using Structs defined in Header files C

I make the struct type in header file data.h

 struct student{
    float tutFee;
};
struct employees{
    float salary;
};
struct person{
    char firstName[10];
    char type; //s for student //e for employee
    union {
        struct student student;
        struct employees employ;
    }/*EDIT ->*/common;
};

then when i attempt to declare a struct of type person in menu.c file

    #include "menu.h"
    #include "data.h"

    int initateProgram(){
        struct person temp;
    }

it gives me an error saying

menu.c:25:19: error: storage size of 'temp' isn't known

Which leads me to believe that either menu.c for some reason does not have access to data.h or i am declaring the struct wrong any insight is appreciated

EDIT

Added a name to the union in the above code. Still is giving me the error i am compiling it as follows

gcc -o a2 uni_personal.c menu.c

uni_personal.c is the main file which calls the function initateProgram() in menu.c Attempting to declare a struct in either location still gets me an error Thanks for the help so Far

Edit 2

I still get the error but when I simplify the program it goes away so evidently the error is unrelated to this particular code

There is nothing wrong with the declaration of struct person temp; in your program. The reason for the "storage size of '...' isn't known" errors is that the compiler sees only a forward declaration of the type, but not the actual definition. Your code, on the other hand, is perfectly valid, assuming that the "data.h" file included from your menu.c file is the file that you show at the top of your post. You can check if that is the case by running only the preprocessor state of your compiler, and examining the output. This can be accomplished by passing a compiler-specific flag ( -E for gcc ).

Before the question was edited: You see this problem because your definition of the union is not compliant with the C standard that your compiler is using (pre- C11), because the union member has no name. Prior to C11, some compilers, such as gcc, supported anonymous unions as a compiler extension.

Adding a name to it should fix the problem with C99-compliant compilers:

struct person{
    char firstName[10];
    char type; //s for student //e for employee
    union {
        struct student student;
        struct employees employ;
    } common; // <<== Here
};

There is nothing wrong with the code you have shown, but you have ensure you compile it correctly. Assuming gcc compiler, either enable gcc extensions, or by compiling the code as standard C (strongly recommended):

gcc -std=c11 -pedantic-errors -Wall -Wextra

If you are using an older version of the standard such as C90 or C99, check the answer by dasblinkenlight.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM