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

This is a discussion on compile error: storage size of 'var' isn't known within the C Programming forums, part of the General Programming Boards category; Hello everyone, I get a strange compile error when compile such simple program. Any ideas? foo.c: In function `main': foo.c:5: ...

  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,682
    So how did you compile your program. did you linked your library with your main program while compiling.

    ssharish2005

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 12:12 AM
  4. Vigenere Decipher/Encipher
    By Xander in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2002, 08: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, 07:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21