'ptrArrMain' is a pointer array that contains two pointer arrays (ptrArr1 and ptrArr2). I have a string ab = "ab". The address of ab[1] (i.e the address of 'b') is stored in the element ptrArr1[1]. ptrArr1[0] (i.e the address of 'a') is assigned to ptrArrMain[0].
How do I get the address of ab[1] using only the ptrArrMain array? I do not wish to use any STLs or precoded functions. I am doing this exercise to enhance my understanding of pointers. Thank you.
int main()
{
string ab = "ab";
string cd = "cd";
char **ptrArrMain = new char*[2];
char **ptrArr1 = new char*[ab.length()];
char **ptrArr2 = new char*[cd.length()];
ptrArr1[0] = &ab[0];
ptrArr1[1] = &ab[1];
ptrArr2[0] = &cd[0];
ptrArr2[1] = &cd[1];
ptrArrMain[0] = ptrArr1[0];
ptrArrMain[1] = ptrArr2[0];
cout << &ab[1] << endl;
// TODO
// Get the address of ab[1] using 'ptrArrMain'.
// Do not use any other array.*/
}
I think this should be possible, because ptrArrMain[0] contains the address of the first element of "ab". With the address of the first element of "ab", I should be able to get the address of ab[1] by incrementing (or some other way) the address of ab[0] which is in ptrArrMain[0].