I have just started learning C++. I was looking at one example of the Accessing an array and the example was as the following:
a[i][j];
*(&a[0] [0]+2*i+j); /* Base address */
*(*(a+i)+j);
I am a bit confused about this example. Why can't I just create a standard loop like the following:
for(int row=0; row<i; row++ ){
for(int col=0; col<j; col++ ){
// Do something
}
}
Your example lists 3 different methods to access an object at (presumably) the same index in a multi-dimensional array. This is something you would do inside a loop. So either do:
for(int row=0; row<i; row++ ){
for(int col=0; col<j; col++ ){
auto field = a[col][row];
}
}
OR
for(int row=0; row<i; row++ ){
for(int col=0; col<j; col++ ){
auto field = *(*(a+col)+row);
}
}
OR
for(int row=0; row<i; row++ ){
for(int col=0; col<j; col++ ){
auto field = *(&a[0] [0]+2*col+row);
}
}