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

having problems with this typedef statement

Discussion in 'C' started by shakisparki, Mar 30, 2011.

  1. shakisparki

    shakisparki New Member

    Joined:
    Mar 30, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hello , Please what does the second typedef statement do,

    struct listnode{
    char data;
    struct listnode *nextPtr;
    };

    typedef struct listnode Listnode;
    typedef ListNode *ListNodePtr;

    what is the difference between

    ListNodePtr *sPtr;
    ListNodePtr startPtr;

    Please explain what the second Line of Code would do please
    startPtr = NULL;
    ListNodePtr *sPtr = &startPtr
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    You posted the same question as an articles two more times and try to avoid it.
     
  3. shakisparki

    shakisparki New Member

    Joined:
    Mar 30, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    It was a mistake? can you please answer my post.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    The typedef defines ListNodePtr as a new type that is equivalent to ListNode *.
    So the definitions:
    Code:
    ListNode *p1;
    ListNodePtr p2;
    
    are equivalent.

    The difference between the next two is that one is a thing and the other is a pointer to that thing. This is true for any type.
    Code:
    TYPE someVar;
    TYPE *ptr2summat;
    
    ptr2summat is defined here as a pointer to TYPE, and someVar is an instance of TYPE.

    Remember that defining a pointer to something DOES NOT create the something. So this is wrong:
    Code:
    int *ptr2int;
    *ptr2int=5;
    
    because ptr2int doesn't point anywhere, but this is valid:
    Code:
    int someInts[10];
    int *ptr2int=&someInts[3]; // ptr2int points to the 4th element
    *ptr2int=5;
    someInts[3]=5; // equivalent to the above line
    
    The second line of code (ListNodePtr *sPtr = &startPtr ;) defines a pointer to ListNotePtr and assigns the address of startPtr to it. sPtr here is a pointer to a pointer to ListNode, so if you want to access the ListNode at the end, you must use **sPtr.
     
  5. teacher

    teacher New Member

    Joined:
    Mar 27, 2011
    Messages:
    66
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    i hate jobs
    Location:
    india,agra
    Home Page:
    Your first doubt
    simple replace Listnode with struct listnode now you have
    typedef struct listnode * ListNodePtr;
    now i can write
    ListNodePtr x;
    and x can contain the address of object of type struct listnode
    for eg
    Code:
    ListNodePtr x;
    ListNode y;
    x=&y;
    
    This is the same thing as
    Code:
    struct ListNode y;
    struct ListNode *x;
    x=&y;
    x->data='e';
    x->nextPtr=NULL;
    
    in these type of problems where typedef is used and you are in confusion always try to replace as i have done above
     
    Last edited: Mar 31, 2011
  6. teacher

    teacher New Member

    Joined:
    Mar 27, 2011
    Messages:
    66
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    i hate jobs
    Location:
    india,agra
    Home Page:
    srry for late reply your second answer same as above try to replace typedef with their actual defination
    you have...
    ListNodePtr *sPtr;
    ListNodePtr startPtr;

    after replacing you get

    ListNode **sPtr
    ListNode *startPtr

    again replace Listnode with struct listnode
    struct listnode **sPtr
    struct listnode *startPtr

    so sPtr is a pointer to a pointer means it will carry the address of another pointer
    so sPtr=&startPtr

    to refernce your data you can use either of them
    startPtr->data or
    sPtr->startPtr->data
     
    shabbir likes this.
  7. teacher

    teacher New Member

    Joined:
    Mar 27, 2011
    Messages:
    66
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    i hate jobs
    Location:
    india,agra
    Home Page:
    now if you have understand your second doubt then you probably have understood the solution of your third problem
    again simply replace with their original definations
    Code:
    ListNodePtr *sPtr = &startPtr;
    
    first replace ListNodePtr
    Code:
    ListNode **sPtr = &startPtr;
    
    and then you have
    Code:
    struct listnode **sPtr = &startPtr; 
    
    again you have something similar to your second doubt
     
  8. shakisparki

    shakisparki New Member

    Joined:
    Mar 30, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    thank y'all, i think i understand now . very resourceful.
     

Share This Page