Invalid types 'int [int]' for array index

This code generates the compilation error indicated in the header, can someone tell me what to change?

#include <iostream>

using namespace std;

int main(){

    int myArray[10][10][10];

    for (int i = 0; i <= 9; ++i){
        for (int t = 0; t <=9; ++t){            
            for (int x = 0; x <= 9; ++x){
                for (int y = 0; y <= 9; ++y){

                myArray[i][t][x][y] = i+t+x+y; //This will give each element a value

                      }
                      }
                      }
                      }

    for (int i = 0; i <= 9; ++i){
        for (int t = 0; t <=9; ++t){
            for (int x = 0; x <= 9; ++x){
                for (int y = 0; y <= 9; ++y){

                cout << myArray[i][t][x][y] << endl;

                    }
                    }
                    }                
                    }

    system("pause");

}

early

+5
source share
5 answers

You sign a three-dimensional array myArray[10][10][10]four times myArray[i][t][x][y]. You probably need to add another dimension to your array. Also consider a container like Boost.MultiArray , although this is possible above your head at the moment.

+11
source

What to change? Besides the problem with a 3 or 4-dimensional array, you must get rid of magic numbers (10 and 9).

const int DIM_SIZE = 10;
int myArray[DIM_SIZE][DIM_SIZE][DIM_SIZE];

for (int i = 0; i < DIM_SIZE; ++i){
    for (int t = 0; t < DIM_SIZE; ++t){            
        for (int x = 0; x < DIM_SIZE; ++x){
+5
source

3- 4 de-refer

4 int myArray[10][10][10][10];

+1
int myArray[10][10][10];

int myArray[10][10][10][10];
+1

, intialized 3D-, 4- .

0

All Articles