A simple question on structures

This is a discussion on A simple question on structures within the C Programming forums, part of the General Programming Boards category; Can you please tell me the difference between these Code: typedef struct aStructure { int a; } object; Code: typedef ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    113

    A simple question on structures

    Can you please tell me the difference between these

    Code:
    typedef struct aStructure
    {
             int a;
    } object;
    Code:
    typedef struct
    {
         int a;
    } aStructure;
    Code:
    struct aStructure
    {
           int a;
    } obj;
    I am a bit confused..

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The first one is a typedef'd struct; the type is "object", and it is defined as a struct aStructure. The second one is the same, except the struct is anonymous, because it does not need a name if it is always going to used via the typedef (in this case "aStructure"). A common situation where the struct does need a name is if it requires an internal pointer to itself, eg:

    Code:
    typedef struct aStructure
    {
             int a;
             struct aStructure *p;
    } object;
    Linked lists work this way; the issue is you cannot use the typedef name ("object *p") inside the definition unless the definition follows the declaration:

    Code:
    struct astruct;
    typedef struct astruct object;
    struct eg {
    	object *p;
    };
    In which case you need a named struct anyway.




    The third one is a struct definition which includes an instantiation, "obj". Ie, obj is a variable of type struct astructure. This is a little confusing because the first two examples, with the typedefs, use the same notation to name the typedef.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little question about structures...
    By mfm12002 in forum C Programming
    Replies: 4
    Last Post: 12-17-2009, 03:05 PM
  2. Pointers and Data Structures (Simple Question, I think)
    By Flamefury in forum C Programming
    Replies: 16
    Last Post: 11-22-2009, 11:17 PM
  3. Question on Structures in C - a different one
    By gvs in forum C Programming
    Replies: 5
    Last Post: 02-09-2005, 01:23 AM
  4. Structures: a yes/no question.
    By Matt13 in forum C Programming
    Replies: 2
    Last Post: 07-29-2004, 08:33 AM
  5. Question about structures
    By Randoon in forum C Programming
    Replies: 2
    Last Post: 12-12-2002, 10:47 PM

Tags for this Thread


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