• Re: What is the problem of array subscript?

    From Richard Damon@21:1/5 to wij on Sun Jul 14 07:09:47 2024
    On 7/14/24 5:21 AM, wij wrote:
    Why g++ reports "error: invalid types ‘unsigned int[int]’ for array subscript"?

    --------------------------
    void t() {
    int z[3][3][3] = {
    {{1,2,3}, {4,5,6}, {7,8,9}},
    {{11,12,13}, {14,15,16}, {17,18,19}},
    {{21,22,23}, {24,25,16}, {27,28,29}},
    };

    cout << z[1][2][0]; // OK

    for(unsigned int x=0; x<3; ++x) {
    for(unsigned int y=0; y<3; ++y) {
    for(unsigned int z=0; z<3; ++z) {

    This z hides the array z, so below the z refers to the single int, not
    the array

    // cout << z[x][y][z];
    cout << z[1][2][0]; // g++ error: invalid types ‘unsigned int[int]’ for array subscript
    }
    }
    }
    }




    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paavo Helde@21:1/5 to wij on Sun Jul 14 14:44:11 2024
    On 14.07.2024 12:21, wij wrote:
    Why g++ reports "error: invalid types ‘unsigned int[int]’ for array subscript"?

    --------------------------
    void t() {
    int z[3][3][3] = {
    {{1,2,3}, {4,5,6}, {7,8,9}},
    {{11,12,13}, {14,15,16}, {17,18,19}},
    {{21,22,23}, {24,25,16}, {27,28,29}},
    };

    cout << z[1][2][0]; // OK

    for(unsigned int x=0; x<3; ++x) {
    for(unsigned int y=0; y<3; ++y) {
    for(unsigned int z=0; z<3; ++z) {
    // cout << z[x][y][z];
    cout << z[1][2][0]; // g++ error: invalid types ‘unsigned int[int]’ for array subscript
    }
    }
    }
    }

    Suggesting to add g++ options -Wshadow -Werror for getting better error messages and avoiding such issues.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)