-1

I have a function in C that one of its arguments is 3D array. when I'm debugging its seems the VS see it as a 2D array, why is it?

this is a program that helps to order goods in containers (boxes). this specific function is called when its known the this spot is ok to put' and it put it on the 3D matrix.the code:

void put_box(unsigned char space[241][47][48], int x, int y, int  z, BOX box, int box_number) {
    int i, j, k;
    int top_stop;
    //cheking if to mark all the hight
    if (box.top)
        top_stop = 48;
    else
        top_stop = box.height / 5;
    // mark the box

    for (i = x; i < x + (box.length / 5); i++)
        for (j = y; j < y + (box.width / 5); j++)
            for (k = z; k < top_stop; k++)
                space[i][j][k] = (unsigned char)box_number;

}/*void put_box*/ 

I expect to see on the locals window the 3D array but I see only 2. and its effect the writing to the matrix.

marcolz
  • 2,880
  • 2
  • 23
  • 28

2 Answers2

3

When you pass arrays to functions, they are passed as pointers. More specifically pointer to the arrays first element.

So for the argument declaration unsigned char space[241][47][48] the compiler really sees it as unsigned char (*space)[47][48].

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Which is why the declaration is exchangeable with `put_box(unsigned char space[][47][48])`, i.e. the first dimension can purely optionally be given but is entirely ignored.The code in the function does not know how many elements are following the first one. – Peter - Reinstate Monica Oct 16 '19 at 06:23
2

Probably because the first array dimension is not part of the func prototype, so the debugger is likely to be looking at the first slice only.

void put_box(unsigned char space[][47][48], <snip> )

I'm guessing that if however you stick a breakpoint in this method, you'd see the full array (but you'll only see one slice when you step into put_box)

int main()
{
  unsigned char space[99][47][48];
  put_box(space, <snip>);
  return 0;
}
robthebloke
  • 9,331
  • 9
  • 12