Thread: structure and typedef

  1. #1
    Registered User
    Join Date
    Feb 2012

    structure and typedef

    I have seen the following code and was surprised
    #include <stdio.h>
    int main()
       typedef struct name name1;
       struct name
           int i;
           char array[10];
     name1 name2;
     name2.i = 13;
     return 0;
    In the line typedef struct name name1, the name structure is not yet defined and why the compiler is not giving any error. Also i cannot define a structure within a structure that is cannot reference itself but i can define a pointer to reference itself. Can someone please explain me what is the logic involved in all these things.

    Thanks and regards,

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    typedef merely renames things; it does not require any information about what that thing is. As long as that thing gets defined, everyone's happy.

    As to the second question, of course a structure cannot contain a copy of itself, because that copy must then contain another copy of itself, and that copy must then contain another copy of itself, .... A pointer is a different type of data, and does not cause an infinite loop as the above would.

  3. #3
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    A structure inside of the same structure. Thinking about it hurts my head...
    struct foo
        struct foo _foo;
    struct foo bar;
    bar._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo._foo = bar;
    Oh... that's beautiful.
    How to ask smart questions
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. typedef structure help
    By bos1234 in forum C Programming
    Replies: 5
    Last Post: 06-05-2012, 07:46 AM
  2. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  3. Structure - Typedef accessing problem
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 08-11-2006, 11:52 PM
  4. Structure and typedef
    By Roaring_Tiger in forum C Programming
    Replies: 6
    Last Post: 03-12-2003, 08:27 PM
  5. structure & typedef
    By beely in forum C Programming
    Replies: 9
    Last Post: 10-30-2002, 08:48 PM