Pointer Trouble -
i trying basic pointer manipulation , have issue clarified. here code snippet referring to
int arr[3] = {0}; *(arr+0) = 12; *(arr+1) = 24; *(arr+2) = 74; *(arr+3) = 55; cout<<*(arr+3)<<"\t"<<(long)(arr+3)<<endl; //cout<<"address of array arr : "<<arr<<endl; cout<<(long)(arr+0)<<"\t"<<(long)(arr+1)<<"\t"<<(long)(arr+2)<<endl;; for(int i=0;i<4;i++) cout<<*(arr+i)<<"\t"<<i<<"\t"<<(long)(arr+i)<<endl; //*(arr+3) = 55; cout<<*(arr+3)<<endl<<endl; my problem is: when try acces arr+3 outside for-loop , desired value 55 printed. when try access through loop, different value(3 in case). after loop, printing value 4. explain me happening? in advance..
you have created array of size 3 , trying access 4th element. outcome therefore undefined.
since allocate array in stack, first time try write 4th element, writing beyond space allocated stack. in debug mode work, in release program crash.
the second time reading value @ 4th place reading value 4. makes sense, compiler has allocated stack space after array variable i, after loop has finished executing have value 4.
Comments
Post a Comment