Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Self Referential Pointer in c [closed]

Tags:

c

pointers

Can someone shed some light on self-referential pointer. Self-referential data types are the types containing the pointer or reference to their own kind but i want know about self-referential pointer. Can you please explain the term with some code.

Any links/resources to learn more about this would also be highly helpful.

Edit1:

Can you explain this:

The only self-referential pointer would be a void *

And also there is this code which confuses me more:

int *p = (int*) &p;

like image 403
rimalroshan Avatar asked Apr 09 '26 11:04

rimalroshan


1 Answers

Other than void*, there is no such thing as a self-referential pointer in C. That's because, if you were to write

foo* p;
foo* pp = &p;

where pp is a self-referential pointer, the types are not compatible, so compilation will fail. The only type for which compilation would pass is the case where foo is void and that's because you are allowed to convert a void** pointer to a void* pointer.

Really, you mean a self-referential type, such as can be found when implementing a linked list.

like image 106
Bathsheba Avatar answered Apr 11 '26 00:04

Bathsheba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!