Thread: typedef .....

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    typedef .....

    What does this mean

    [insert]
    Code:
    typedef struct elementT 
    {
          int data;
          struct elementT *link;
    
    } element;
    My thinking goes that elementT is a structure which has these two elements (data and a pointer link). But what is this term element after the braces signify and for what purpose it is used?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by roaan
    My thinking goes that elementT is a structure which has these two elements (data and a pointer link).
    No, struct elementT is the name of the structure type, and element is an alias of the structure type.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by laserlight View Post
    No, struct elementT is the name of the structure type, and element is an alias of the structure type.
    And elementT itself is an identifier which lives in the "structure tag" namespace. This identifier doesn't specify a type, but when prefixed with struct it does. Welcome to C land
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Okay so that implies i can make declarations like

    element S1, S2 ;
    S1 -> data = some value
    S1 -> next = some address // all this is ok

    but this is

    elementT S1, S2;
    S1 ->data = some value
    S1 -> next = some address // this is wrong

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by roaan View Post
    Okay so that implies i can make declarations like

    element S1, S2 ;
    S1 -> data = some value
    S1 -> next = some address // all this is ok

    but this is

    elementT S1, S2;
    S1 ->data = some value
    S1 -> next = some address // this is wrong
    Correct. But you COULD do:

    Code:
    struct elementT S1, S2;   
    S1 ->data = some value
    S1 -> next = some address // this is wrong
    The purpose of the typedef is to create a more convenient alias for "struct elementT" instead of saying "struct" everywhere.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Also the only way a structure can make use of itself within its defined structure is with the use of pointer. This is an alias to how linked lists are created and thus what we call an ADT (abstract data type).

    Using S1->next is not correct as you have not defined what next is. In your example you will simply call

    Code:
       element *next;
       S1->link = next  /*Or some other pointer to an elementT structure */

  7. #7
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Oh i meant that

    S1 ->link = some address

    Just one more stuff till now i have only been using for linked lists declarations of type

    [insert]
    Code:
    struct node 
    {
               int data;
              struct node *link;
    };
    and used it like

    struct node *start;

    What is the advantage of typedef over this declaration.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    What is the advantage of typedef over this declaration.
    There isn't much of an advantage. It just saves you from a little extra typing.

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Yeah, using typedefs for structs is a more intuitive measure to help define, what your struct is and how it is to be used. Simply stated - it saves in coding a bit as you do not have to use the keyword struct with every declaration call.

  10. #10
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    You'll see what advantage this brings. Many times 7 letters ("struct" plus space) and easier maintainability. (what if your user-defined "struct IMAGE" suddenly becomes an int for some obscure reason? Or your opaque "file_t" now uses stdio.h instead of file-descriptors?)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM