Can a pointer point to a memory address in C?

In the following code, the pointer points to its own memory address.

#include <stdio.h>

int main() 
{
    void * ptr;
    ptr = &ptr;

    return 0;
}

Does it make sense if the pointer could point to its own memory address?

+4
source share
4 answers

No, that doesn't make sense. If you can find the ptr variable, you can just do & ptr. This will give you the same results as the contents of ptr.

Moreover, since ptr is just telling something about itself, it is useless. It does not provide any information that makes sense for the rest of your program.

, . , ptr == & ptr , NULL. .

, & ptr , -, NULL.

+7

.

, "100%", " ".

+5

, C. - . - - .

+2

, , , .
ptr void *, &ptr void ** ptr - (void * vs. void **).
C ++, , , , void *, a void * .
, ptr , ( ) ,

*(void **)ptr = ...

, - .

-3

Source: https://habr.com/ru/post/1684016/


All Articles