Discussion:
What is the problem of array subscript?
(too old to reply)
wij
2024-07-14 09:21:19 UTC
Permalink
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
}
}
}
}
Richard Damon
2024-07-14 11:09:47 UTC
Permalink
Post by wij
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
Post by wij
// cout << z[x][y][z];
cout << z[1][2][0]; // g++ error: invalid types ‘unsigned int[int]’ for array subscript
}
}
}
}
wij
2024-07-14 12:14:57 UTC
Permalink
Post by Richard Damon
Post by wij
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
Thanks. The recent g++ in my PC often reports strange messages.
Post by Richard Damon
Post by wij
        // cout << z[x][y][z];
        cout << z[1][2][0];   // g++ error: invalid types ‘unsigned int[int]’ for array subscript
      }
    }
  }
}
Paavo Helde
2024-07-14 11:44:11 UTC
Permalink
Post by wij
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.
Loading...