Thread: compile error: storage size of 'var' isn't known

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

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

    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;
    }
    goo.c

    Code:
    struct st {
    
    int member;
    
    };
    goo.h

    Code:
    typedef struct st t_st;

    thanks in advance,
    George

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    So how did you compile your program. did you linked your library with your main program while compiling.

    ssharish2005

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You define the structure in a .c file. How can any other module possibly know about it?

    Both the structure and the typedef should be in goo.h, and there should be no file goo.c.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well goo.c has the actual structure, so there is no way for foo.c to know what the members are.

    All that goo.h does is basically confirm that a struct called st exists.

    In foo.c, you could have
    t_st *var;
    but you still would not be able to access any members within that struct.

    It is, in other words an incomplete type.

    Incomplete types are very useful for implementing opaque interfaces. goo.h declares an opaque pointer, which any user just sees the pointer.
    The actual content of the struct is private to goo.c.

    It's what FILE* should be - all user code just passes around a FILE* without ever knowing what is behind it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Definitely, yes! Any ideas?


    Quote Originally Posted by ssharish2005 View Post
    So how did you compile your program. did you linked your library with your main program while compiling.

    ssharish2005

    regards,
    George

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks, do you mean any structure definition (real implementation) must be in .h in C? I have seen many program written by others which define (implement) struct in .c other than .h.


    Quote Originally Posted by brewbuck View Post
    You define the structure in a .c file. How can any other module possibly know about it?

    Both the structure and the typedef should be in goo.h, and there should be no file goo.c.

    regards,
    George

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Salem, do you mean in order to access the member of a struct, we have to define struct in .h and includes it, or else we could only access its member variable inside the same .c file? No walk-arounds?


    Quote Originally Posted by Salem View Post
    Well goo.c has the actual structure, so there is no way for foo.c to know what the members are.

    All that goo.h does is basically confirm that a struct called st exists.

    In foo.c, you could have
    t_st *var;
    but you still would not be able to access any members within that struct.

    It is, in other words an incomplete type.

    Incomplete types are very useful for implementing opaque interfaces. goo.h declares an opaque pointer, which any user just sees the pointer.
    The actual content of the struct is private to goo.c.

    It's what FILE* should be - all user code just passes around a FILE* without ever knowing what is behind it.

    regards,
    George

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes.
    For direct access, you need the structure name and its members in scope.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    Thumbs up

    Hey,
    Can u use resource of any person to whom u dont know.
    Same is happening here. Ur program knows goo.h and goo.h has no information about goo.c. even foo.c knows nothing about goo.c. So how can foo.c access goo.c resource.
    S_ccess is waiting for u. Go Ahead, put u there.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Is it so hard to use English rather than that cryptic "kiddie" speak?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help assignment
    By 6kaine9 in forum C Programming
    Replies: 26
    Last Post: 10-19-2008, 08:51 PM
  2. size of Object c++ compile problem
    By micha_mondeli in forum C++ Programming
    Replies: 5
    Last Post: 04-06-2005, 01:20 PM
  3. server question
    By xddxogm3 in forum Tech Board
    Replies: 24
    Last Post: 01-21-2004, 01:12 AM
  4. Vigenere Decipher/Encipher
    By Xander in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2002, 09:24 AM
  5. File Size and File Size on Disk
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-15-2001, 08:03 PM