7
                         |--------|     
//                     / |4  4  4 |     
//                   |--------| 4 | 
//                 / |3  3  3 | 4 | 
//               |---------|3 |   |
//             / | 2  2  2 |3 | /
//            |---------|2 |__|
//            | 1  1  1 |2 | /
//            | 1  1  1 |__| 
//            | 1  1  1 | /
//            |_________|
double arr[4][3][3] = {{1,1,1,1,1,1,1,1,1},{2,2,2,2,2,2,2,2,2},{3,3,3,3,3,3,3,3,3},{4,4,4,4,4,4,4,4,4}};

I consider that this array consists of 4 layers.

I want to create pointer to layer of array and traverse through layers of that array using pointer.

I try :

double (*pp1)[sizeof(arr[0]) / sizeof(ar[0][0][0])]; 
pp1 = arr[0]; 

and get error from intelIsense: value of type (double (*)(3) can`t be assigned to double(*)(9)

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
spin_eight
  • 3,925
  • 10
  • 39
  • 61

3 Answers3

1

So if you do:

int i;
double arr[4][3][3] = {{1,1,1,1,1,1,1,1,1},{2,2,2,2,2,2,2,2,2},
                       {3,3,3,3,3,3,3,3,3},{4,4,4,4,4,4,4,4,4}};

double (*pp3)[3][3];    
pp3 = arr;    
for (i = 0; i <= 3; i++)    
{   printf("pp3 + %d is %f \n", i, ***(pp3 + i));
}   

Then you get the desired behavior. The problem with your code is that, just as the compiler is telling you, you are trying to assign a pointer to an array of 9 double (double (*pp1)[sizeof(arr[0] / sizeof(arr[0][0][0])] evaluates to double (*pp1)[9] ), but you need a pointer to an array of 3 of array of 3, which is what you declared with double arr[4][3][3]. From my tests, gcc will accept the double (*pp1)[9] with a compiler warning, which is what I tried to get at in my comment below. Hope that clears things up.

If you want to keep this general, then what you really want is double (*pp3)[sizeof(arr[0]) / sizeof(arr[0][0])][sizeof(arr[0][0]) / sizeof(arr[0][0][0])], which is a bloody nightmare.

EDIT: Forgot a dereference... Should've copy/pasted haha. Also added explanation about the question's code behavior. Fixed as per comments.

groundlar
  • 878
  • 1
  • 8
  • 17
  • On second thought, it's proper to do `(*pp3)[3][3]`, which is a pointer to an array of 3 of array of 3, which is what you have. The original (using `(*pp3)[9]`) works, but since we don't really have an array of 9, I believe that's where the error was coming from... – groundlar Sep 14 '12 at 11:51
  • If you want to keep this general, then what you really want is `double (*pp3)[sizeof(arr[0]) / sizeof(arr[0][0])][sizeof(arr[0][0]) / sizeof(arr[0][0][0])]`, which is a bloody nightmare. Gotta agree with using proper data structures... but this will do what you asked, properly. I'll add this to the answer. – groundlar Sep 14 '12 at 12:28
  • double (*pp3)[3][3]; pp3 = arr[0]; wan`t compile. to compile code you have to declare pointer as: double (*pp3)[3] - because arr[0] is pointer to the first element of arr, which has dimensions [3][3], as you declaring pointer on this element, it is also points to first element of [3][3] array, which is (*pp3)[3]. Alexey Frunze gave reference on very detailed explanation of usage of pointers to arrays, here it is http://stackoverflow.com/a/12385772/968261 – spin_eight Sep 17 '12 at 11:02
  • Sorry, that was an artifact from a prior revision. It should be `(*pp3)[3][3]; pp3 = arr;` . I should really copy/paste instead of retyping... I'll fix that. – groundlar Sep 17 '12 at 13:57
0

While trying to figure out this issue i got some results.

I find out that the following statements raises no errors from compiler

double arr[4][4][9];
double (*pp3)[9];
pp3 = arr[0];

So, is it right to deduce from code above that pointer can be assigned to array only in case if number of elements to which it points is equall to the smallest dimension of array?

update: I think that pointer can be created on the smallest layer of array only.

Can somebody explain this?

spin_eight
  • 3,925
  • 10
  • 39
  • 61
  • @Alexey Frunze- yes, thanks a lot for the good source of information. Now I understand how to use pointers on arrays, which is quite clearly explained in answer you gave me. – spin_eight Sep 17 '12 at 10:51
0

Why not simply get the direction of the first element of a given layer?

double *pp1 = &ar[0][0][0];
double *pp2 = &ar[1][0][0];
/* and so on */
Genís
  • 1,468
  • 2
  • 13
  • 24