Groups | Blog | Home
all groups > visual c > march 2007 >

visual c : compile error: storage size of 'var' isn't known


George
3/28/2007 8:52:04 AM
Hello everyone,


I get a strange compile error when compile such simple program. Any ideas?

foo.c: In function `main':
foo.c:5: error: storage size of 'var' isn't known

foo.c

[CODE]
#include "goo.h"

int main (int argc, char** argv)
{
t_st var;
var.member = 100;
return 0;
}
[/CODE]

goo.c

[CODE]
struct st {

int member;

};

[/CODE]

goo.h

[CODE]
typedef struct st t_st;
[/CODE]


thanks in advance,
Bo Persson
3/28/2007 7:21:08 PM
[quoted text, click to view]
:: Hello everyone,
::
::
:: I get a strange compile error when compile such simple program. Any
:: ideas?
::
:: foo.c: In function `main':
:: foo.c:5: error: storage size of 'var' isn't known

The compiler can figure out that 'var' is of type 't_st', which is another
name for a struct named 'st'. However, it cannot see what 'st' is, as it is
hidden in another translation unit.

You can have a pointer to st without knowing exactly what it is, but you
cannot have a variable of type st without seeing the full definition.


Bo Persson

::
:: foo.c
::
:: [CODE]
:: #include "goo.h"
::
:: int main (int argc, char** argv)
:: {
:: t_st var;
:: var.member = 100;
:: return 0;
:: }
:: [/CODE]
::
:: goo.c
::
:: [CODE]
:: struct st {
::
:: int member;
::
:: };
::
:: [/CODE]
::
:: goo.h
::
:: [CODE]
:: typedef struct st t_st;
:: [/CODE]
::
::
:: thanks in advance,
:: George


George
4/4/2007 1:18:01 AM
Thanks Bo Persson!


I want to confirm that, if I define a struct in .c file, I can only use the
struct in the same .c file, and have no walk-around to utilize the struct in
other .c file.

If I want to use the struct in multiple .c files, I have to define it in .h
file?


regards,
George

[quoted text, click to view]
Bo Persson
4/4/2007 5:51:41 PM
[quoted text, click to view]
:: Thanks Bo Persson!
::
::
:: I want to confirm that, if I define a struct in .c file, I can only
:: use the struct in the same .c file, and have no walk-around to
:: utilize the struct in other .c file.

The "work-around" is to define the struct identically in both .c files. That
in effect simulates what happens when you include a .h file in both .c
files.

Not recommended! :-)

::
:: If I want to use the struct in multiple .c files, I have to define
:: it in .h file?

This is the reason for having .h files in the first place. It lets you share
declarations between several implementation files.


Bo Persson

AddThis Social Bookmark Button