Unexpected Results When Using Pointers to Access Array Elements : <p>I have two classes.  <code>class1</code> and <code>class2</code>.  <code>class2</code> creates an array with elements that are of type <code>class1</code>.  <code>class2</code> has a member that points to that array.  I can access the pointer with <code>class 2's</code> <code>getpointer</code> method, but I get unexpected results when I try to use the pointer to access members of the array's elements.</p>

<p>Here's the code:</p>

<pre><code>#include &lt;iostream&gt;
using namespace std;

class class1{
    int a;
    int b;
public:
    class1(){}
    class1(int x, int y){a = x; b = y;}
    int geta(){return a;}
    int getb(){return b;}

};

class class2{
    int c;
    int d;
    class1 *e;
public:
    class2(int x, int y){c = x; d = y;}
    void setE(){

        class1 arr[3];

        class1 arrTemp (0,0);
        arr[0] = arrTemp;

        class1 arrTemp1 (1,1);
        arr[1] = arrTemp1;

        class1 arrTemp2 (2,2);
        arr[2] = arrTemp2;


        cout &lt;&lt; "Element 1, A = ";
        cout &lt;&lt; arr[0].geta() &lt;&lt; endl;

        cout &lt;&lt; "Element 1, B = ";
        cout &lt;&lt; arr[0].getb() &lt;&lt; endl;

        cout &lt;&lt; "Element 2, A = ";
        cout &lt;&lt; arr[1].geta() &lt;&lt; endl;

        cout &lt;&lt; "Element 2, B = ";
        cout &lt;&lt; arr[1].getb() &lt;&lt; endl;     


        cout &lt;&lt; "Element 3, A = ";
        cout &lt;&lt; arr[2].geta() &lt;&lt; endl;

        cout &lt;&lt; "Element 3, B = ";
        cout &lt;&lt; arr[2].getb() &lt;&lt; endl;        

        class1 *pt;
        pt = &amp;arr[0];

        e = pt;

        }
    class1* getpointer(){return e;}
    };

int main(){

        class2 testObj (1,2);

        testObj.setE();

        class1 *objPointer = testObj.getpointer(); 

        class1 tempclass1;

        tempclass1 = *(objPointer + 0);
        cout &lt;&lt; "Element 1, A = ";
        cout &lt;&lt; tempclass1.geta() &lt;&lt; endl;

        cout &lt;&lt; "Element 1, B = ";
        cout &lt;&lt; tempclass1.getb() &lt;&lt; endl;

        tempclass1 = *(objPointer + 1);
        cout &lt;&lt; "Element 2, A = ";
        cout &lt;&lt; tempclass1.geta() &lt;&lt; endl;

        cout &lt;&lt; "Element 2, B = ";
        cout &lt;&lt; tempclass1.getb() &lt;&lt; endl;  

        tempclass1 = *(objPointer + 1);
        cout &lt;&lt; "Element 3, A = ";
        cout &lt;&lt; tempclass1.geta() &lt;&lt; endl;

        cout &lt;&lt; "Element 3, B = ";
        cout &lt;&lt; tempclass1.getb() &lt;&lt; endl;             



    };
</code></pre>

<p>This is the output:</p>

<pre><code>Element 1, A = 0
Element 1, B = 0
Element 2, A = 1
Element 2, B = 1
Element 3, A = 2
Element 3, B = 2
Element 1, A = 0
Element 1, B = 0
Element 2, A = -360531408
Element 2, B = 29391
Element 3, A = -360531408
Element 3, B = 29391
</code></pre>

<p>With the first set of <code>cout</code> statements I verify that the data is going into the array correctly.  </p>

<p>It looks like I get the correct data for the first element, but after that what I get with the pointer is completely different from the actual array.  I'm very new to C++, but I thought that <code>*(Pointer + i)</code> was equal to <code>arr[i]</code>.</p>
