Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any functional difference between (*p)->some_var and *p->some_var

Tags:

c

I have seen several C code examples which utilise the (*p)->some_var way of working with pointers (using parens around the pointer).

My question is, is there any functional difference between the two methods of working with C pointers, and if so, how did this difference come about?

like image 543
stay_frosty Avatar asked Dec 02 '22 11:12

stay_frosty


1 Answers

Those do not do the same thing at all. Compare:

(*p)->some_var // aka (*(*p)).some_var

This means "p is a pointer, which we dereference, then dereference again to access a field."

*p->some_var // aka *((*p).some_var)

This means "p is a pointer, which we dereference to access a field, which is a pointer which we dereference."

like image 103
John Zwinck Avatar answered Dec 04 '22 00:12

John Zwinck