Sunday, August 2, 2009

Int b[2]={0,1}; int* p; p = b; p = p+1; Now see details?

Now C++ automatically converts the line p=p+1 with p = p+1*sizeof(int). Is there any method to make C++ not do this.

Int b[2]={0,1}; int* p; p = b; p = p+1; Now see details?
No, it is the basis of typed pointer arithmetics. p is of type int*, so an incrementation in p makes address vary of sizeof(int).
Reply:If you want your pointer to point to the next byte in memory:


((char*)p) +=1;
Reply:It is designed to do this. But if you want to add 1 to b[0] then you can do *p=*p+1. Or if you want the pointer to point to an increased memory location, then you can try p=(int *)((int)p+1)


No comments:

Post a Comment