Learn

Fixing "invalid types for array subscript" in Arduino

Beginner

Or see every lesson.

  • 505parts on the bench
  • 45boards running now
  • 1.00xreal time, on every board
Arduino Uno: read a knob, ready to breaklive0.000 s 0.00x
Click to open it in the editor
It prints the knob’s reading five times a second. Change Serial.print(raw); to Serial.print(raw[0]); and press Run: subscripted value is not an array, pointer, or vector.

invalid types 'int[int]' for array subscript means square brackets were put after something that is not an array. The part before the brackets is the type of the thing, and the part inside is the type of the index: int[int] is an int used as if it were an array, char[int] a char, float[int] a float. The other form, int [5][float], is a real array with a float as its index, which C++ does not allow.

Mokxi compiles with clang, which says subscripted value is not an array, pointer, or vector for the first kind and array subscript is not an integer for the second. The circuit above reads a knob on A0 and prints the reading. Open it, change Serial.print(raw); to Serial.print(raw[0]); and press Run: raw is one int, not an array.

"int[int]": brackets on a plain variable

The demo’s break: raw holds one reading, so raw[0] asks for the first box of something that has no boxes. Clang said subscripted value is not an array, pointer, or vector on line 24, and GCC 13 says invalid types 'int[int]' for array subscript. A char gives invalid types 'char[int]' for array subscript and a float invalid types 'float[int]' for array subscript. Clang’s words are the same for all three, and Mokxi’s build tab says: "Square brackets [ ] go after an array, and this is a single value. Remove the brackets, or declare it as an array with its size, as in int readings[5];. Check too for a second variable with the same name nearer this line."

Either the brackets do not belong, and Serial.print(raw); is what you meant, or the variable was meant to be an array and was declared as one value. An array is declared with its size in brackets: int readings[5];.

A local variable with the array’s name

Declare int readings[5] at the top of the sketch and then int readings = 0; inside a function, and inside that function the name means the new int. The array is hidden, and readings[2] gave GCC’s invalid types 'int[int]' for array subscript and clang’s subscripted value is not an array, pointer, or vector, though the array is right there at the top. Give the local variable another name, such as total.

The same happens with a function’s parameter. int total(int values, int n) that adds up values[i] gave the same two messages, because values is one int. Declare it as an array: int total(int values[], int n). That version, given {4, 5, 6}, printed 15.

Averaging an array of readings
int readings[5] = {500, 510, 520, 505, 515};

void setup() {
  Serial.begin(9600);
  long sum = 0;
  for (int i = 0; i < 5; i++) {
    sum += readings[i];
  }
  Serial.println(sum / 5);
}

void loop() {
}

Open this sketch in the editorThe code alone: the editor places the board and the parts it can read from the sketch.

"int [5][float]": a float as the index

An index counts boxes, so it has to be a whole number. values[i] with float i = 2; gave GCC’s invalid types 'int [5][float]' for array subscript and clang’s array subscript is not an integer, which the build tab explains as "An array index counts boxes, so it has to be a whole number." Make the counter an int, or convert it where you use it: values[(int)i] printed 30, the third of {10, 20, 30, 40, 50}. A float worked out from a sensor reading can be 2.9999 rather than 3, so pass it through round() first if it came from arithmetic.

Two indexes on an array with one

grid[1][2] on int grid[3] takes one box with grid[1], an int, and then tries a second index on it: GCC said invalid types 'int[int]' for array subscript. A table of rows and columns is declared with two sizes, int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};, and then grid[1][2] printed 6, the last number of the second row.

An index past the end is a different problem

This error is about the type of the thing before the brackets, never about the number inside them. An index that is too big has the right types, so it builds. With a fixed number the compiler can see the problem and warns: values[9] on int values[5] built here with clang’s array index 9 is past the end of the array (that has type 'int[5]'), and a note, array 'values' declared here.

With a variable as the index it says nothing at all, and the sketch reads whatever is in memory after the array, or writes over it, which can make the board misbehave in ways that look unrelated. The boxes of int values[5] are numbered 0 to 4, so a loop over it runs for (int i = 0; i < 5; i++), with < and not <=.

Brackets that do work: String and pointers

A String takes brackets, because the class allows it: word[0] on String word = "Mokxi" printed M, and word[1] gave o. So does a pointer, which is how an array passed into a function arrives. If the brackets fail on a value you expected to be one of these, check how it was declared, and look for a second declaration with the same name nearer the line.

Brackets after a function’s name are a different slip, a call written with the wrong brackets: millis[0] gave clang’s subscript of pointer to function type 'uint32_t ()' (aka 'unsigned long ()'). Use round brackets to call it, millis().

Open this circuit in the editor. Arduino Uno: read a knob, ready to break, wired as it runs at the top of this page. Change a part or a value and run it again.

Open the circuit

Questions

What does "invalid types 'int[int]' for array subscript" mean?

Square brackets were used after an int that is not an array. Either remove the brackets, or declare the variable as an array with its size, int readings[5];. Also check for a second variable with the same name.

What does "invalid types 'int [5][float]' for array subscript" mean?

The array is real but its index is a float. Use an int as the index, or convert it: values[(int)i].

Why does it say char[int]?

The thing before the brackets is a single char, not a char array or a String. Declare a char array, char name[10], or use a String, which can be indexed.

What does Mokxi say instead?

Mokxi compiles with clang: subscripted value is not an array, pointer, or vector for brackets on a plain value, and array subscript is not an integer for a float index.

Sources

Where the facts on this page come from

Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.