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.