0

I am trying to manipulate the array prarr[3][3] by using a pointer ptr_ptr_prarr to an array ptr_prarr[3][3] that contains pointers to prarr[3][3].

I would like to somehow point ptr_ptr_prarr to ptr_prarr and access the data in prarr[i][j] by using something like ptr_ptr_prarr[i][j].

void main()
{
int prarr[3][3];
int count = 1;

for (int i = 0; i <= 2; i++)
    for (int j = 0; j <= 2; j++)
        prarr[i][j] = count++;

//printArray(prarr);

int *ptr_prarr[3][3];
for (int i = 0; i <= 2; i++)
    for (int j = 0; j <= 2; j++)
        ptr_prarr[i][j] = &prarr[i][j];

int n = 1, x = n, y = n;
printf("\nArray Coords:(%d,%d)\nMemory location:%p\nValue:%d", 
        x, y, ptr_prarr[x][y], *ptr_prarr[x][y]);

int **ptr_ptr_prarr= *ptr_prarr;
printf("\n");
for (int i = 0; i <= 8; i+=3)/*  <<---------------------------------  */
{
    for (int j = 0; j <= 2; j= (j+1))
        printf("%d", (ptr_ptr_prarr[i][j]));
    printf("\n");
}

Sleep(20000);
}

I think I've gotten close but for some reason ptr_ptr_prarr requires the line containing "<<---------------------------------" : to iterate in a different fashion in order to replicate the original data.

Somehow I'm accessing something wrong and any help would be greatly appreciated!

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Pointer is little complex, but you are making it worst. Why don't go simple? – haccks Aug 18 '17 at 21:22
  • Tip; Questions about 2D arrays are better illustrated by using different sizes like `int prarr[2][3];` – chux - Reinstate Monica Aug 18 '17 at 21:53
  • It's a bit hard to understand why you use this pointer array. If it is not just for experimenting with pointers, could you please describe what you actually want to achieve? Anyway, `for (int i = 0; i <= 8; i+=3)` together with `ptr_ptr_prarr[i][j])`, where your compete program does not contain any array larger than `[3][3]` is suspicious. – Stephan Lechner Aug 18 '17 at 21:55
  • This is in fact just for experimentation. This is just to flesh out my understanding of pointers and arrays. – Connor Carr Aug 18 '17 at 22:01

2 Answers2

0

There's no need to use an address-of operator on array since arrays decay into implicit pointers on the right-hand side of the assignment operator. So

int **ptr_ptr_prarr= ptr_prarr;

Pointer to Array of Pointers

Lucas Hendren
  • 2,786
  • 2
  • 18
  • 33
0

I think the problem lies with this line...

int **ptr_ptr_prarr= *ptr_prarr;

This creates a double pointer to the address of prarr[0][0]. What I think you are trying to do is this...

int **ptr_ptr_prarr= ptr_prarr;

This creates a pointer to the ptr_prarr pointer, which can then be dereferenced to get the original array.

Doing it this way, if you want to modify prarr using ptr_ptr_prarr, you can do something like

**ptr_ptr_prarr = 10

This would modify the original array so that prarr[0][0] now contains the value 10.

**(ptr_ptr_prarr + 1) = 10

This would modify the original array so that prarr[0][1] now contains the value 10.

Overall, this is a poor use of pointers. This doesn't seem particularly useful, nor do I think it helps to understand pointers any better. If you are trying to use this piece of code in an actual program (and not just as an example), I suggest finding a different way to do it.

Alakazooom
  • 126
  • 4
  • Thank you. I came across this problem while working on something else a few days ago but in that project I simply found a different way to do it. I came back to the problem today because I thought it could further my understanding of pointers. The solution you provided was actually one of the first things I tried, but I assumed it wouldn't work because my compiler spit the error..."warning C4047: 'initializing': 'int **' differs in levels of indirection from 'int *(*)[3]' "...and I thought there must be a flaw in my understanding. It works though. I should've ignored my compiler. Thanks! – Connor Carr Aug 18 '17 at 22:09
  • Well, the compiler is right. It will work (just about anything works in C), it just might produce some weird behavior if you don't know exactly what you are doing. To be honest, ptr_prarr should either be an array that holds addresses, or a pointer to the array (that you can increment as needed). Since you are trying to practice with pointers, I would recommend the second option. That way you actually have some practice with double pointers. – Alakazooom Aug 18 '17 at 22:37