11

This following code gets this compile error: "invalid types 'int[int]' for array subscript". What should be changed?

#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");

}

7 Answers 7

19

You are subscripting a three-dimensional array myArray[10][10][10] four times myArray[i][t][x][y]. You will probably need to add another dimension to your array. Also consider a container like Boost.MultiArray, though that's probably over your head at this point.

Sign up to request clarification or add additional context in comments.

1 Comment

dammit, I must have accidentally deleted that line earlier on before i started trying to run it.
7

What to change? Aside from the 3 or 4 dimensional array problem, you should get rid of the 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){

1 Comment

Just in case in future you have a different number of fingers ;-)
5

Just for completeness, this error can happen also in a different situation: when you declare an array in an outer scope, but declare another variable with the same name in an inner scope, shadowing the array. Then, when you try to index the array, you are actually accessing the variable in the inner scope, which might not even be an array, or it might be an array with fewer dimensions.

Example:

int a[10];  // a global scope

void f(int a)   // a declared in local scope, overshadows a in global scope
{
  printf("%d", a[0]);  // you trying to access the array a, but actually addressing local argument a
}

1 Comment

Just wanted to let you know, you saved me hours, thanks!
3
int myArray[10][10][10];

should be

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

Comments

2

You're trying to access a 3 dimensional array with 4 de-references

You only need 3 loops instead of 4, or int myArray[10][10][10][10];

Comments

1

I think that you had intialized a 3d array but you are trying to access an array with 4 dimension.

1 Comment

My advice when giving answers is to try and point to specific parts of the original code from the question. In your case it would mean including int myArray[10][10][10]; for the 3-dimensional array reference and myArray[i][t][x][y] = i+t+x+y; for thr 4-domensional array reference. Sometimes people will downvote if they don't have a context to understand your answer.
0

I just forgot to put [] to the function parameter with array data type like this:

🚨 Instead of this:

void addToDB(int arr) {
    // code
}

✅ Do this:

void addToDB(int arr[]) {
    // code
}

Comments

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.