3

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;

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
rooni
  • 1,036
  • 3
  • 17
  • 33
  • 1
    You'll need to be more specific about what you're asking. It's trivial to do `void* p = &p`, but I assume you're asking about something more complex (and less useless). – Sneftel May 10 '18 at 13:34
  • You seem to refer to a term ("self-referential pointer") you have read or heard somewhere. Could you refer to that source to give some context? – Yunnosch May 10 '18 at 13:35
  • See https://stackoverflow.com/questions/39151133/what-is-self-referencing-pointer-in-c – benjarobin May 10 '18 at 13:36
  • @Sneftel i want to know more about the term self-referential-pointer and nothing complex – rooni May 10 '18 at 13:39
  • @Yunnosch https://stackoverflow.com/questions/39151133/what-is-self-referencing-pointer-in-c this also has the same title – rooni May 10 '18 at 13:40
  • @rimiro The term "self referential pointer" does not have a widely used meaning other than a self-referential type. – Sneftel May 10 '18 at 13:43
  • 1
    @rimiro The question you linked seems to explain it pretty well. What *specifically* about it are you having trouble understanding? – dbush May 10 '18 at 13:47
  • 1
    In the `int *p = (int *) &p;`, the cast is necessary because `&p` has the type `int **` which is not compatible with `int *`, but the cast tells the compiler "I know what I'm doing". The `void *p; p = &p;` example also features a type conversion, but any data pointer type can be converted to `void *` without cast. The type of `&p` is `void **`, but the implicit conversion to `void *` is permitted by C. – Jonathan Leffler May 10 '18 at 14:13

2 Answers2

2

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.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

You can construct a self referencing pointer this way:

void *p = &p;

p is a generic pointer that points to itself.

The problem is you cannot dereference this pointer without a cast to an actual pointer type. Here is code to demonstrate this:

#include <stdio.h>

int main() {
    void *p = &p;
    printf("p = %p\n", p);
    printf("*(void **)p = %p\n", *(void **)p);
    printf("*(void **)p %c= p\n", *(void **)p == p ? '=' : '!');
    return 0;
}

As commented by Jonathan Leffler, void *p; p = &p; features a type conversion, but any data pointer type can be converted to void * without a cast. The type of &p is void **, but the implicit conversion to void * is permitted by C.

There are also self referring types:

struct self { struct self *self; };

This structure is not very useful, it is a simple indirection, but you can initialize it in a self referring way:

#include <stdio.h>

struct self { struct self *self; };

int main() {
    struct self s = { &s };
    struct self *p = &s;

    printf("p = %p\n", (void *)p);
    printf("p->self = %p\n", (void *)p->self);
    printf("p %c= p->self\n", p == p->self ? '=' : '!');
    return 0;
}

In both cases, p contains its own address, which can be interpreted as p is self-referential.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 1
    Note that the `void *p; p = &p;` example features a type conversion, but any data pointer type can be converted to `void *` without a cast. The type of `&p` is `void **`, but the implicit conversion to `void *` is permitted by C. – Jonathan Leffler May 10 '18 at 14:14
  • "Self-referential data types are the types containing the pointer or reference to their own kind" in the same way how would you define self-referential pointers. – rooni May 10 '18 at 14:21
  • @rimiro You're looking for knowledge where there is nothing to know. The only thing that would make a pointer "self-referential" is pointing to itself. – Sneftel May 10 '18 at 14:22