Newbie questions about pointers and linked list

This is a discussion on Newbie questions about pointers and linked list within the C Programming forums, part of the General Programming Boards category; Hello there, Suppose I've got the following structure: Code: typedef struct s_no *p_no; struct s_no { int value; p_no next; ...

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    52

    Newbie questions about pointers and linked list

    Hello there,

    Suppose I've got the following structure:
    Code:
    typedef struct s_no *p_no;
    
    struct s_no {
      int value;
      p_no next;
    };
    And:
    Code:
    void insert_beginning(p_no *pL, p_no y)
    {
      y->next = *pL;
      *pL = y;
    }
    This function is used like this:
    Code:
    p_no L = NULL, x;
    
    p_no x = (p_no) malloc(sizeof(struct s_no));
    
    insert_beginning(&L, x);
    My doubt is, after calling that function, L and x will store the same address, and also L->next will be the same as x->next, right?

    So what's the difference between the function above and this one, where instead of passing &L to it I'd pass only L:

    Code:
    void insert_beginning(p_no pL, p_no y)
    {
      y->next = pL;
      pL = y;
    }
    I tried it but the results were different, L stores the 0x0 address while x stores something else, but I couldn't figure out why.

    I appreciate any help.

  2. #2
    Left
    Join Date
    Oct 2011
    Posts
    816
    I don't know about others here, but I personally detest defining pointer types. I personally want to see the asterisk * whenever a variable is really a pointer; it just makes it easier to read and parse the code. I almost skipped your question because of that..

    Quote Originally Posted by koplersky View Post
    Code:
    p_no L = NULL, x;
    p_no x = (p_no) malloc(sizeof(struct s_no));
    You're declaring x twice. Typo, I presume?

    Quote Originally Posted by koplersky View Post
    My doubt is, after calling that function, L and x will store the same address, and also L->next will be the same as x->next, right?
    If L == x, and they are of the same type (p_no), then by definition L->next == x->next , yes.

    Quote Originally Posted by koplersky View Post
    So what's the difference between the function above and this one, where instead of passing &L to it I'd pass only L:
    In that case the list pL is passed by value (instead of by reference as earlier). Therefore, pL = y only affects pL in the current scope (within this function); in the caller, L will not be modified.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    52
    Thank you!

    Yes, it was typo... and about defining pointer types, the code is not mine, I was just studying it. But now you've mentioned it, I also think it's not good to define them, because using * all the time makes it easier for one to look and immediately identifying the pointer type.

    So let me see if I got it right, this line:
    Code:
    y->next = *pL;
    can modify x->next in the caller because I'm passing the node that x points to by reference, but I can't modify x itself (from the function) because I'm passing it by value. Is that correct?

    Thanks again.
    Last edited by koplersky; 10-07-2012 at 07:58 PM.

  4. #4
    Left
    Join Date
    Oct 2011
    Posts
    816
    Perhaps it would be easier to read when written like this:
    Code:
    struct node {
        struct node *next;
        int       value;
    }
    
    void insert_beginning(struct node **list, struct node *one)
    {
        one->next = *list;
        *list = one;
    }
    Quote Originally Posted by koplersky View Post
    So let me see if I got it right, this line:
    Code:
    y->next = *pL;
    can modify x->next in the caller because I'm passing the node that x points to by reference, but I can't modify x itself (from the function) because I'm passing it by value. Is that correct?
    Yes, that is exactly correct!

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    52
    Okay, that really cleared things up.
    Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. managing a list -newbie questions-
    By Cenema in forum C Programming
    Replies: 2
    Last Post: 08-28-2010, 08:07 AM
  2. linked list newbie
    By sababa.sababa in forum C Programming
    Replies: 15
    Last Post: 12-07-2009, 12:59 PM
  3. Pointers confusing me (Newbie questions)
    By klawson88 in forum C Programming
    Replies: 12
    Last Post: 04-30-2009, 01:14 AM
  4. newbie: pointers to structure questions
    By cstudent in forum C Programming
    Replies: 11
    Last Post: 04-09-2008, 06:23 AM
  5. linked list questions...
    By revelation437 in forum C++ Programming
    Replies: 6
    Last Post: 04-22-2003, 04:42 PM

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