Calling an array of arrays in C.

Corona688 was great in helping me learn how to create arrays that hold other two dimensional array here. Unfortunately I didn't think ask about how to implement or call them. Basically, I'm trying to call an array of two-dimensional arrays like this:

declaration:

int (*side_one[MATRIX][MATRIX])[MATRIX] = { { white_l1, white_l2, white_l3 }, { white_m1, white_m2, white_m3 }, { white_r1, white_r2, white_r3 } };

call:

fillit((*side_one[MATRIX][MATRIX])[MATRIX]);

gcc warnings:

gcc -O4 test.c -o test -s
test.c: In function �fillit':
test.c:63:16: warning: assignment to �int *' from �int' makes pointer from integer without a cast [-Wint-conversion]
    *side[t] = randval;
                ^
test.c: In function �main':
test.c:457:40: warning: passing argument 1 of �fillit' makes pointer from integer without a cast [-Wint-conversion]
      fillit((*side_one[MATRIX][MATRIX])[MATRIX]);
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
test.c:53:19: note: expected �int * (*)[1000][1000]' but argument is of type �int'
 void fillit(int *(side[MATRIX][MATRIX])[MATRIX]){
             ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I've tried changing the argument in this function, how I call the function, and a lot of things, but no luck. Here is the function the function call goes to:

void fillit(int *(side[MATRIX][MATRIX])[MATRIX]){
  int r, s, t, randval;

  for (r = 0; side[r] != 0; r++){     
     for (s = 0; s < MATRIX; s++){
    FILE *f;
    for (t = 0; t < MATRIX; t++) {
        f = fopen("/dev/urandom", "r");
        fread(&side, sizeof(randval), 1, f);
        if (side[t] != NULL){
            *side[t] = randval;
        }
        fclose(f);
    }
     }
   }
}

I'm sure the loops in the function are wrong. Sure it can probably be done in less than 3 loops. I won't be mad to get suggestions on those, but right now I'm just trying to pass the argument correctly.

Any advice much appreciated.

The C programming language was not really designed with multi-dimensional arrays in mind. C sort of works with 2D arrays, but remember that they are either arrays of arrays or arrays of pointers to arrays. Get this wrong, and you will get lots of errors, or worse, segmentation faults.

3D arrays are a nightmare! Don't do it in C! Use a language better suited to multi-dimensional arrays, or an OO language with a tried-and-tested multi-dimensional array class. If you have to use C, don't use 3D arrays - emulate them instead!

typedef struct arr_str{
     int *arr;  /* pointer to array */
     int xDim; /* size of X dimension */ 
     int yDim; /* size of Y dimension */
     int zDim; /* size of Z dimension */
} threeDeeArr, *threeDeeArrPtr;

int getIndex(threeDeeArrPtr array, int x, int y, int z)
{
   int index;
   index = x * array.xDim;
   index = (index + y) * array.yDim;
   return index + z;
}

int getFromIndex(threeDeeArrPtr array, int x, int y, int z)
{
   return array->arr[getIndex(array, x, y, z)];
}

void putAtIndex(threeDeeArrPtr array, int x, int y, int z, int val)
{
   array->arr[getIndex(array, x, y, z)] = val;
}

my calculations of a 3D array are probably off, you will have to test that I got it right. The above code does not include the initialisation or destruction "methods" required before you can actually use this struct in anger. Once the indexing is correctly calculated the above struct will be much easier to use in C than proper 3D arrays.

Andrew

Thank you for the response apmcd47. I was beginning to wonder if anyone would reply. However, I'm not trying to make a 3D array. Either that or I have my terminology wrong. What I'm trying to achieve is a 1D array that holds references to 2D arrays and be able to call them.

I believe I've already got them initialized with the code already mentioned:

int (*side_one[MATRIX][MATRIX])[MATRIX] = { { white_l1, white_l2, white_l3 }, { white_m1, white_m2, white_m3 }, { white_r1, white_r2, white_r3 } };

Which was in this post that was mentioned to. Perhaps Corona688 provide me with a 3D array in that post, but I though (*side[][])[] was different than (*side[][][]), no?

If you know a better way to call lists of two-dimensional arrays I would be more than happy to learn this. However, I'm afraid you may have confused this post with a 3D array instead.

Either way, thank you for the reply!

EDIT: Also curious why you would not recommend C for arrays of 3D or larger? I understand that C supports up to 10 dimensions of arrays, which is more than any other language I know. Perhaps you were making this recommendation due to easy of use? If so, I'm not sure that a higher level language would be good for the rest of my code, but if you have suggestions I would be open to hear which ones.

Sorry, my mistake. I saw the three sets of [] and didn't notice the () around two of the brackets.

Yes, you are right, I had indeed mistaken it for a 3D array.

I had not known about the 10 dimensions of arrays. I certainly would not have met that limit as, as I have said, I would have emulated a multi-dimensional array much sooner. Part of my reasoning is the fact that C's handling of arrays, arrays of arrays, etc are so similar from the programmer's point of view, that it can be confusing and lead to the problems you are experiencing. I'm afraid I have no recommendations for you.

Looking at your compiler error, I'm, wondering if the line

*side[t] = randval

should be

**side[t] = randval

Andrew

Thank you for the answer back. It does seem that **side[s][t] = randval helped resolve that part of the problem. Before I go over what's left I changed the last [] to ROW as it should only have three for the three {} in these I guess:

int (*side_one[MATRIX][MATRIX])[ROW] = { { white_l1, white_l2, white_l3 }, { white_m1, white_m2, white_m3 }, { white_r1, white_r2, white_r3 } };

So I have a define for that as '3' and these are my warnings/errors now:

$ gcc test.c -o test
test.c: In function �main':
test.c:457:39: warning: passing argument 1 of �fillit' from incompatible pointer type [-Wincompatible-pointer-types]
      fillit((side_one[MATRIX][MATRIX])[ROW]);
                                       ^
test.c:54:19: note: expected �int * (*)[1000][3]' but argument is of type �int *'
 void fillit(int *(side[MATRIX][MATRIX])[ROW]){
             ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~

Not really sure. I've tried moving around the * signs inside and outside of the parenthesis. Even tried removing them. Basically I've tried to brute-force this problem, but I seem to get the same results.

Thanks for help with the pointer to the pointer though!

Here's how I had to use it:

#include <stdio.h>

void arrbyptr(int (*ptr[2][2])[2]) {

        printf("[0][0][0]=%d,[0][0][1]=%d,[0][1][0]=%d,[0][1][1]=%d\n",
                (*ptr)[0][0][0],
                (*ptr)[0][0][1],
                (*ptr)[0][1][0],
                (*ptr)[0][1][1]);

        printf("[1][0][0]=%d,[1][0][1]=%d,[1][1][0]=%d,[1][1][1]=%d\n",
                (*ptr)[1][0][0],
                (*ptr)[1][0][1],
                (*ptr)[1][1][0],
                (*ptr)[1][1][1]);


}

int main(void)
{
        int arr2d[2][2]={ {1,2},{3,4} };
        int arr2db[2][2]={ {5,6},{7,8} };

        int (*ptr[2][2])[2]= { arr2d, arr2db };

        arrbyptr(ptr);
}

There's an extra layer of indirection for the pointer pointers, but it wasn't quite where I expected it to be.

1 Like

That is awesome. It will probably be later tonight before I have time to test and merge that with what I've got. I'll update here later when I've had time to try that out and make sure I'm not still lost.

Wanted to say thank you again. Especially to Corona688, RudyC and other long time members. With a name like unix.com I would expect more learning about OS Administration, which this site has too. At the same time I've learned more about C here than on some of the bigger dev forums that only do dev stuff.

I'll update this soon once I get time to test this :slight_smile:

EDIT: Following the last method did work.

1 Like