Confusion about typedef and pointers in linked lists

This is a discussion on Confusion about typedef and pointers in linked lists within the C Programming forums, part of the General Programming Boards category; Hi everybody! I was trying to do some linked lists exercises, and I'd like to ask you what is the ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    4

    Confusion about typedef and pointers in linked lists

    Hi everybody!

    I was trying to do some linked lists exercises, and I'd like to ask you what is the difference between the following two prototypes?
    Code:
    void insert(ListNodePtr sPtr, char value);
    void insert(ListNodePtr *sPtr, char value);
    Is one of this wrong? The meaning is the same or the second one means that sPtr is a pointer to a second pointer?

    These were the definitions of the linked list components:
    Code:
    typedef struct listNode ListNode;
    typedef ListNode *ListNodePtr;
    
    struct listNode {
     char data;
     ListNodePtr nextPtr;
    };

    Thanks,

    Diego
    Last edited by mrdiegus; 11-17-2012 at 03:16 PM.

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    For nodes, you'd want to use listNode *sPtr as the parameter. Pointers are many times more flexible, though I am a bit less experienced than I want to be, so I can't really explain exactly why this is better.
    Code:
    void insert(listNode *sPtr, char value);
    and if you want a list that can be connected, you want a pointer to another listnode inside the struct definition.
    Code:
    struct listNode {
    
     char data;
    
     listNode *nextPtr;
    
    };

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    But having used typedef, ListNodePtr is already a pointer, so, shy should I use ListNodePtr *sPtr instead of ListNodePtr sPtr? I suppose anyway that the firs version is much better (the one you suggested), since in the Deitel and Deitel C how to program, the examples use that version....

    Thanks,
    Diego

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by mrdiegus View Post
    I was trying to do some linked lists exercises, and I'd like to ask you what is the difference between the following two prototypes?
    Code:
    void insert(ListNodePtr sPtr, char value);
    void insert(ListNodePtr *sPtr, char value);
    Is one of this wrong? The meaning is the same or the second one means that sPtr is a pointer to a second pointer?
    There is a big difference.
    The first version takes a pointer to ListNode. If you change the pointer inside the function (so it points to somewhere else, i.e. sPtr = xxxx) the change won't be visible to the caller. For example if you have this code:
    Code:
    ListNodePtr head = NULL;
    printf("%p\n", (void *) head);
    insert(head, 'a');
    printf("%p\n", (void *) head);
    "head" will still point to NULL after calling the function no matter how you change "sPtr" within insert().

    In your second version, you pass a pointer to pointer to ListNode, thus changes to the pointer to ListNode (*sPtr = xxxx) will be visible to the caller.

    You wouldn't say
    Code:
    void func(int value);
    void func(int *value);
    are the same, would you? It's the same when you use pointers in function calls. Arguments are always passed by value in C.

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Andreas, thanks a lot.

    Following the reasoning, are the following equivalent?

    Code:
    void insert(ListNodePtr *sPtr, char value); // given the typedef written before
    void insert(ListNode **sPtr, char value); // replacing typedef with original ones
    Thanks again,

    Diego

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Yes, they are . But there are some reasons for you not to typedef the pointers

    - What do you mean by saying some reasons?I wouldn't be convinced and nor should you.
    Take a look at my thread here .The problem was that i couldn't see because of typedef that ListPtr was actually a point..My eyes expected to see an asterisk..And so i allocated memory for a pointer, not for where the pointer points to ..
    Last edited by std10093; 11-19-2012 at 10:18 AM.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Std10093,

    thanks, finally I've understood the matter... I agree with you, the two equivalent prototypes are the evidence, since the latter is much more readable!

    Thanks,
    Diego

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. typedef confusion (LINKED LIST)
    By bos1234 in forum C Programming
    Replies: 1
    Last Post: 03-24-2011, 08:24 AM
  2. Help with pointers and linked lists
    By Christine in forum C Programming
    Replies: 3
    Last Post: 02-13-2011, 10:21 AM
  3. Help with linked lists/ pointers
    By anything25 in forum C Programming
    Replies: 14
    Last Post: 06-26-2009, 02:41 PM
  4. Double pointers and linked lists.
    By simo_r in forum C Programming
    Replies: 2
    Last Post: 05-06-2008, 04:25 AM
  5. Linked Lists & Pointers
    By fkheng in forum C Programming
    Replies: 4
    Last Post: 06-10-2003, 07:26 AM

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