C++ Pointers – Recursive Self-Referencing

cpointers

I have been learning C++ recently, and upon reading up on pointers I had a moment of thought.

I'm still attempting to grasp the very idea of pointers so excuse me if this doesn't make sense beyond comprehension.

Is it possible for a set of pointers recursively point into one another's memory location upon trying to print out the value of what they point at.

This should be done without needing to create this in a "fake" way of creating them in a never ending loop.

It should indefinitely point into infinity when trying to access a pointer's pointed at value. Is this a feasible "thing" to create?

Best Answer

That's not how pointers work.

A pointer is, essentially, a record of a memory address. If dereferenced, you can read (or write) the value that lives at that address.

Pointers can point to anything that lives in memory. In particular, they can point to other pointers. They can even point themselves (although that is pretty useless).

However, the action of dereferencing a pointer must be taken explicitly. A pointer that points to another pointer yields the value of that other pointer when you dereference it, not automatically. And if that other pointer points to a third thing, reaching that third thing takes two explicit actions.

A pointer doesn't magically turn into the thing pointed to in your hands. Therefore it isn't possible to create reference structures that compute forever without having an explicit loop in the mix. Such a loop can run endlessly, like any loop with an insufficient termination condition, but you do need the loop, not just the pointers.