Compile error i can't figure out
im getting a "storage size of ‘data’ isn’t known" when i try to use a certain struct.
Code:
ioHelper.h:
#ifndef IOHELPER_H_
#define IOHELPER_H_
typedef struct fileValues data;
struct fi开发者_StackOverflowleValues ioInput(FILE* file,int dim,int sign);
#endif /* IOHELPER_H_ */
ioHelper.c:
struct fileValues
{
int dim;
char sign;
double x;
double y;
};
map.c:
void drawData(FILE* vectors)
{
double paramLoc [MAX_DIMENSION];
char sign;
(this is where i get the error) struct fileValues data;
...
}
any ideas?
This is because when compiling map.c the compiler can't see the full definition of the struct in IoHelper.c.
You probably only included IoHelper.h, which has the (incomplete) declaration, not the definition.
So you cannot declare a variable of that struct type inside of map.c unless you
- include IoHelper.c (BAD IDEA)
- put the struct definition in IoHelper.h
- declare a pointer to the struct in map.c and malloc it.
Assuming that map.c is not including IoHelper.c, it only sees the typedef but not the declaration of struct fileValues. Because it didn't see the declaration, it can't figure out how big the structure is, hence the compile error.
Normally you would declare a struct in a header file - move it from iohelper.c to iohelper.h and map.c should now compile.
data
is an incomplete type. which means that it isn't fully defined, so its size is not known. in this case, it's not defined at all, just declared.
you need to make a definition available to code that declares a variable of this type, such as in your function drawData
.
things you can do with an incomplete type include using it as base for a pointer or reference type, and using it as result or by-value argument type in a function declaration (as you did). but you cannot do anything that requires knowing the size. declaring a variable requires knowing the size.
cheers & hth.,
精彩评论