I’m working on a problem that uses pointer arithmetic and I have found this small snippet of code that works. I don’t understand exactly what it’s doing though. To me it looks like it is assigning the address of buffer + the value of ix3 to the array element a[i]. I don’t know why that would be relevant to my program though. Can someone please tell me exactly what is happening in this loop?
int *buffer=new int[5*3];
for (i=0;i<5;i++)
a[i] = buffer+i*3;
The expression
is identical to
so your assumption is correct, and I hope that
a[]is an array of pointers.Note that pointer arithmetic like
buffer+kdoes not take the address value contained inbufferand add k to it: instead, it’s equal to the value of&buffer[k], which should equal the address value contained in buffer + k * sizeof(the type being pointed to by buffer ).