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. 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.. Originally Posted by koplersky Code:
```p_no L = NULL, x;
p_no x = (p_no) malloc(sizeof(struct s_no));```
You're declaring x twice. Typo, I presume? Originally Posted by koplersky 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. Originally Posted by koplersky 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. 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. 4. 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;
}``` Originally Posted by koplersky 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. Okay, that really cleared things up.
Thanks a lot! Popular pages Recent additions 