0

I'm trying to make a multidimensional array, which contains lots of data.

However, I don't know the initializer size of the middle array since this is always different for each array level.

It looks like this:

 char* spawnableVehicles[3][][3] = {
      {
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
      },
      {
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
      },
      {
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
      },
 }

How do I initialize the size of the middle array?

4
  • 2
    You cannot have an array of arrays that are different sizes. Each element of an array must be the same type, including size. (spawnableVehicles[0] cannot be an array of five things while spawnableVehicles[1] is an array of three things.) You must either specify a maximum size for the middle dimension that encompasses all cases, or you must use a different data structure. Commented Jan 6, 2019 at 16:02
  • 1
    You can't have a different size for any inner array. They all must be the same size. So, choose an appropriate maximum and use that: char* spawnableVehicles[3][8][3] ... or stop using your array approach and switch to pointers with malloc() and free(). Commented Jan 6, 2019 at 16:06
  • @EricPostpischil Okay, clear. What would be a appropriate data structure for my case? Commented Jan 6, 2019 at 16:07
  • 1
    @AppelFlap: Your question does not state much about the situation. An array of pointers to arrays might be suitable, with each pointer being set to an address returned by malloc. Commented Jan 6, 2019 at 16:21

1 Answer 1

2

You need to identify all the indices clearly. The definition as spawnableVehicles[3][][3] yields to error while when I defined spawnableVehicles[30][30][30] the error vanished. I also wrote a code to check this out:

#include<iostream>
using namespace std;
int main(){
    char* spawnableVehicles[30][30][30] = {
      {
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
      },
      {
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
      },
      {
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
           {"test1", "test2", "test3"},
      },
 };
 for (int i=0;i<30;i++){
    for (int j=0;j<30;j++){
        for (int k=0;k<30;k++){
            if (spawnableVehicles[i][j][k]!=NULL){
                // printing spawnableVehicles[i][j][k] when it has a value other than NULL
                cout<<i<<"   "<<j<<"   "<<k<<"   "<<spawnableVehicles[i][j][k]<<"\n";
            }
        }
    }
 }
 return 0;
}

with the following result:

0   0   0   test1
0   0   1   test2
0   0   2   test3
0   1   0   test1
0   1   1   test2
0   1   2   test3
0   2   0   test1
0   2   1   test2
0   2   2   test3
0   3   0   test1
0   3   1   test2
0   3   2   test3
0   4   0   test1
0   4   1   test2
0   4   2   test3
1   0   0   test1
1   0   1   test2
1   0   2   test3
1   1   0   test1
1   1   1   test2
1   1   2   test3
1   2   0   test1
1   2   1   test2
1   2   2   test3
2   0   0   test1
2   0   1   test2
2   0   2   test3
2   1   0   test1
2   1   1   test2
2   1   2   test3
2   2   0   test1
2   2   1   test2
2   2   2   test3
2   3   0   test1
2   3   1   test2
2   3   2   test3
2   4   0   test1
2   4   1   test2
2   4   2   test3
2   5   0   test1
2   5   1   test2
2   5   2   test3
2   6   0   test1
2   6   1   test2
2   6   2   test3
2   7   0   test1
2   7   1   test2
2   7   2   test3

--------------------------------
Process exited after 0.08576 seconds with return value 0
Press any key to continue . . .
Sign up to request clarification or add additional context in comments.

4 Comments

Seems to be suitable for my situation. Just a question, how come on the second line it jumps from 4 to 0 again?
Well, in which case you mean? I don't see any 4 to 0 in the second line. Or I misunderstood you...
This is because of the priority of increments in values of i,j,k. Note that first of all, k increases. When no more increment is possible (say, spawnableVehicles[i][j][k] is NULL) the increment happens next in j similarly after that in i. That's why we have a transition from [0 4 2] to [1 0 0] because for all the vectors [i][j][k] with i=0, j>=4 and k>3 we have spawnableVehicles[i][j][k]=0 and the next immediate non-NULL spawnableVehicles[i][j][k] is spawnableVehicles[1][0][0]
I appreciate the answer. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.