Hi Im just starting to get my head around pointers and arrays and Ive more or less know how to manipulate pointers in a one dimensional arrays to display the elements. But how about in multidimensional arrays? I have been practicing with this code:
#include<iostream>
using namespace std;
int main()
{
int a[2][3]= { {1,2,3},{4,5,6}};
int (*ptr)[3] = &a[0]; // or (*ptr)[3] = a;
cout <<"Adress 0,0: "<< a << endl;
cout <<"Adress 0,0: "<< ptr << endl;
cout <<"Value 0,0: "<< *a[0] << endl;
cout <<"Value 0,0: "<< *(ptr)[0]<< endl;
cout <<"Adress 0,1: "<< &a[0][1] << endl;
cout <<"Adress 0,1: "<< (ptr)[1] << endl;
return 0;
}
I have managed to display the address and value of a[0][0] using the array name and the pointer, but how to display the address and value of a[0][1] and the succeeding elements by using the pointer?
(ptr)[1]doesn't point toa[0][1], it points toa[1][0].(*ptr)[1]if you're looking to deliver the value2from your array of arrays. The address of that element is simply(*ptr)+1(or, if you want to be long-winded,*(ptr+0) + 1