1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

declaration issues

Discussion in 'C' started by technosavvy, Jan 18, 2008.

  1. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    Hi

    I have just come across an issue that i am just not able to understand..
    i have declared a structure in one of .c files ..
    e.g.

    Code:
    struct fun {
    char var1;
    int var 2;
    struct fun *next 
    };
    
    now what i have done is i have made a header file and put a typedef like:

    Code:
    typedef struct fun morefun
    and GOD KNOWS how the header file is able to know that the structure has been defined in the other file..and code is compiling with not even a single warning..
     
  2. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    The typedef doesn't do anything by itself, except create an alias for some other kind of declaration (in this case, a struct).

    For example, you can also put this in your header file.
    struct fun;

    Also, it's possible to declare pointers to structures without the body of the structure in scope, so for example
    struct fun *handle;
    This ability allows you to create an interface to your package/module which has an opaque type. That is, the user only deals with a pointer-to-something, but can never dereference it to sneakily change something on the inside. It's like how the FILE* type for handling files in stdio works.

    You only need the actual structure in scope when you create variables with that structure, eg.
    struct fun myvar;
    or when you have a pointer to the struct, and you want to dereference the pointer, eg.
    ptr->member = value;
     
    shabbir likes this.
  3. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    salem...i believe the uestion was not put in an elucid manner...or maybe i have not understood the answer...

    what i was tying to ask was that how come the header file knows that there is a declaration of that structure in some other file ...
    please expain in a bit elaborative way...:)
     
  4. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    It doesn't know, nor does it need to, at that point in the compilation process.

    Like say extern variables and function prototypes tell the compiler about data and functions which are to be declared later, all you're doing is saying that 'morefun' is an alias for 'struct fun'. At that point, you don't need to know any more detail than that.
    It's only when you actually USE the declaration (to declare an instance of that struct say) that you need the complete declaration in scope.

    Consider a recursive structure.
    Code:
    // this is no different to your typedef
    struct bar;
    
    struct foo {
      struct bar *next;  // not an instance, only a pointer
    };
    struct bar {
      struct foo *next;
    };
    
    Because foo only has a pointer to bar, it doesn't need the whole struct in scope to be able to declare foo.
     
  5. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    I found Salem's answer very informative.
    However, if you put this at the top of a working program it will not compile:
    typedef DoesNotExist AnotherNameForDoesNotExist;
    But this will:
    typedef struct DoesNotExist AnotherNameForDoesNotExist;
    So it is really about the "struct" keyword and not "typedef".
    To some degree, all structs are apparently considered to be of the same "type".
    This might relate to C's separate struct namespace, which always seemed kind
    of weird (useless) to me, but there must be some reason.

    As for FILE, in MinGW's stdio.h it is declared like this:
    Code:
    /* The structure underlying the FILE type.
     * I still believe that nobody in their right mind should make use of the
     * internals of this structure. */
    
    typedef struct _iobuf
    {
        char* _ptr;
        int   _cnt;
        char* _base;
        int   _flag;
        int   _file;
        int   _charbuf;
        int   _bufsiz;
        char* _tmpfname;
    } FILE;
    So you could get at it, but there's really no point.
     

Share This Page