2

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

4
  • 2
    Look into the preprocessed form, e.g. with gcc -C -E menu.c > menu.i then load menu.i in your editor. Commented Feb 17, 2015 at 15:06
  • 1
    This compiles fine for me using GCC. Though with -std=c99 -pedantic it warns that unnamed unions are nonstandard. Commented Feb 17, 2015 at 15:07
  • 1
    Though unnamed unions are legal C11. Please show how you compile and make sure the code shown reproduces the problem. Commented Feb 17, 2015 at 15:17
  • 2
    I took the liberty to edit the post to clarify the edit, or one might get confused when reading the already posted answers. Commented Feb 17, 2015 at 15:38

2 Answers 2

9

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

Comments

2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.