As a beginner to c++ I'm struggling to understand pointers and array. I wrote the following program:
int main (void){
int p[3]={0};
int * iptr = new int [4];
iptr++;
*iptr=2;
iptr++;
*iptr=3;
for (int i=0;i<4;i++){
cout << "iptr: " << *iptr << endl;
iptr++;
}
return 0;
}
However I'm not getting the expected results. I would have expected elements [1] and [2] to have the values 2 and 3 respectively. However I get a result along the lines of:
iptr: 3
iptr: -842150451
iptr: -33686019
iptr: 0
Can someone please tell me where I'm going wrong in my understanding?
Thanks Dan