Fox
Fox

Reputation: 593

C++ Pointer Pointing to index of an Array of Objects

Quick questions about pointers. In this example:

  Object *O;

  Object** array = new Object*[3];

  O = array[0];

In this example, Does O point to the spot array[0] or the object located in the position? For example, if that object in index 0 gets swapped to say object in spot 2, I understand that O should still have access to that object not the new one now in array[0], right?

Sorry just want to double check. Been working over a day w/o sleep & starting to question what's what at this point.

Upvotes: 2

Views: 511

Answers (1)

ComicSansMS
ComicSansMS

Reputation: 54589

Your assumptions are correct.

When doing the O = array[0]; assignment (which is equivalent to writing O = (*array);) you copy the pointer stored in the array to your pointer variable O. Any change to the array afterwards won't change the contents of O, since it is a copy.

Upvotes: 1

Related Questions