typedef structures

This is a discussion on typedef structures within the C Programming forums, part of the General Programming Boards category; typedef Struct node { char elem; struct node * next; } StackType; typedef StackType * StackPtr; StackPtr top; is now ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    2

    typedef structures

    typedef Struct node {
    char elem;
    struct node * next;
    } StackType;

    typedef StackType * StackPtr;
    StackPtr top;

    is now StackPtr a type of variable?? and top is a variable of StackPtr type??

    i get confused going from

    struct node {
    int val;
    struct node *next;
    };

    to

    typedef struct node{
    char elem;
    struct node *next;
    }StackType;

    these two structures are essentially the same thing, right???????


    if that is the case, then is the following correct

    Code:
    correcting errors in this fragment:
    
    void push (StackPtr *top, char p)
    {
    StackPtr newptr;
    newptr = malloc();
    newptr.elem = p;
    newptr->next = top;
    top = newptr;
    }

    should be changed to
    Code:
    void push(StackPtr *top, char p)
    {
    StackPtr newptr;
    newptr=malloc(sizeof(StackPtr));
    newptr->elem = p;
    newptr->next = top;
    top = newptr;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void push (StackPtr *top, char p)
    {
    StackPtr newptr;
    newptr = malloc();
    newptr.elem = p;
    newptr->next = top;
    top = newptr;
    }
    This is incorrect. Close, but incorrect. I'll assume you're using pseudo code, in which, this would be correct. It, correctly, would be this:
    Code:
    void push (StackPtr *top, char p)
    {
        StackPtr newptr;
        newptr = malloc( sizeof( StackType ) );
        newPtr->elem = p; /* you're using a pointer, remember */
        newptr->next = top;
        top = newptr;
    }
    Yeah. Your second example is correct.

    using 'typedef' means you're defining a new variable type. I actually prefer to do this when making my nodes:
    Code:
    typedef struct node Node;
    Node {
        Node *n;
        int myVar;
    };
    Nice and clean.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    2
    Quzah, your sizeof was of StackType, mine was sizeof StackPtr.
    Does it make a difference??? Seems it would, now that I think about it, cuz StackType is a structure type and StackPtr is just a variable of that type. Right??

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes, there is a difference.
    The difference is like:

    x = malloc( sizeof( struct ThisStruct ) );
    x = malloc( sizeof( struct ThisStruct* ) );

    The first, you're allocating space for the whole structure.
    The second, you're only allocating space for a _pointer_ to a structure. Big difference.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Sayeh
    Guest
    Instead of using the old, P-style declarations

    Code:
    //  ----p-style----
    
    typedef Struct node { 
    char elem; 
    struct node * next; 
    } StackType; 
    
    typedef StackType * StackPtr; 
    StackPtr top;
    Let's do it this way instead-- much more readable--


    Code:
    typedef Struct node
       { 
       char elem; 
       struct node *next; 
       }StackType,*StackPtr;
    
    StackPtr top;
    enjoy.

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, 11:36 AM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 08:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 06: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, 11:54 AM

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