0

Coming from this and this, it seems to me that dereferencing a pointer is like asking what value an address contains to which the pointer is pointing to, but I cannot understand the same in this case:

#include <stdio.h>

void function(){
    printf("Hello\n");
}

int main(){
    typedef void func(void);
    func* x = &function;
    printf("%p %p %p %p\n", (void *)***********x, (void *)******x, (void *)*****x, (void *)x);
}

which when compiled and run by gcc tests.c; ./a.out produces:

0x55b286fd8149 0x55b286fd8149 0x55b286fd8149 0x55a295e95149

My main question is how these can be apparently infinitely dereferenciable?

Although not necessary, but it would be helpful in understanding if answer is made commenting on why all these addresses are same?

xrfxlp
  • 421
  • 5
  • 15

1 Answers1

1

A function is implicitly convertible to a pointer to itself. E.g. func* x = function; is legal.

So, when you dereference x the second time, the function resulting from the previous dereference is converted to a pointer to itself, which is then deferenced again.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207