[Solved] Can you use double brackets in a nested for loop?


I am not sure what exactly you are stuck on based on your question so I made a minimal C program with comments

I declared an int array who’s first and second dimension are at least 10 because you iterate both i and j from 0 to 9 (inclusive). This is to avoid out of bounds problems while iterating

The array’s elements are not initialized in the program. It is possible that this program will print all zeros when you run it. It is also possible that it prints other values that happened to be in memory (because the array values are not initialized)

Last I declared i and j outside the for loop just in case this was the problem you were facing

#include <stdio.h>

int main(int argc, char** argv) {
    // Declare an array
    // Note that both dimensions are at least 10
    // Also note that none of the values are initialized here
    int myArray[10][10];

    // Both i and j are declared here rather than in the for loop
    // This is to avoid the following potential error:
    // error: 'for' loop initial declarations are only allowed in C99 or C11 mode
    int i, j;

    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++) {
            // Note that the values this prints are uninitialized
            printf("%d\n", myArray[i][j]);
        }
    }

    return 0;
}

solved Can you use double brackets in a nested for loop?