# problem with Pointers and array

• 01-07-2011
platoali
problem with Pointers and array (SOLVED)
Hi,
I'm trying to write a simple program to learn and use array and pointer. It copies values from a vector to an array and prints the value of the array. Here is the portion of the code that causes problem:

Code:

```    int *pArr = new int[vect.size()] ;     int *pArrBegin = pArr ;     for ( vector<int>::iterator itr = vect.begin() ; itr != vect.end() ; itr++ ) {         int *pItr = pArr;         *pItr =  * itr ;         pItr++ ;     }     cout << "Print Array: " << endl ;     for ( int *pArrItr = pArrBegin  , *pArrEnd = pArr + vect.size(); pArrItr != pArrEnd; pArrItr++ ){         cout << * pArrItr << " " ;     }```
Here is the output:
Code:

```  ./copyVectorToArray 1 2 3 4 Print vec: 1 2 3 4 Print Array: 4 0 0 0```
I just don't understand why when I increment pltr, pArr and pArrBegin is also incremented.
• 01-07-2011
hk_mp5kpdw
Code:

```for ( vector<int>::iterator itr = vect.begin() ; itr != vect.end() ; itr++ ) {     int *pItr = pArr;     *pItr =  * itr ;     pItr++ ; }```
Being local to the loop, it gets recreated each iteration of the loop and is always pointing to first slot in the allocated memory at the point of the assignment below what I've marked. Thus, the remaining slots never get filled with data.
• 01-07-2011
platoali
problem with Pointers and array (SOLVED)
Thank you very much. It works now.