In the following code, a pointer points to its own memory address.
#include <stdio.h>
int main()
{
void * ptr;
ptr = &ptr;
return 0;
}
Would it make sense, if the pointer was able to point to its own memory address?
No, it doesn't make sense. If you can find variable ptr, you can just do &ptr. It will give you the same results as the contents of ptr.
Moreover since ptr only tells something about itself, it's useless anyhow. It doesn't provide any info meaningful to the rest of your program.
Come to think of it, there's one exception. You could use the case where ptr == &ptr as a kind of special value, like NULL. So, in that case I would consider it useful.
The fun of it is that in that case the value &ptr makes sense as a special value precisely while it doesn't make sense as the address of something, just like NULL.
Pointer point to its own memory address
It's legal.
Whether it "makes sense" 100%ly depends on the context and though "is primarily option based".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With