C++
C++ Invalid Types Array Subscript Fix
The brackets ask for one element out of many. When the value in front of them holds only one thing - a count, a price, a single character, one enum value - there is no element to pick, and compilation stops with 'invalid types ... for array subscript'. The index is rarely the problem; the value being subscripted is. This guide separates the cases, reads what the two types in the message mean, and matches each one to its fix.
Written November 25, 2026 · Tested with GCC 13.3.0 · 12 min read
- The rule underneath: built-in subscript needs an array or a pointer, and the index needs to be integral
- How to read the diagnostic
- The first failure: an int subscripted as a list
- The second failure: a double subscripted as a list of prices
Written November 25, 2026
Tested with GCC 13.3.0
What this guide is
The subscript in value[index] asks for one element out of a sequence. That only makes sense when the value in front of the brackets is a sequence, a pointer into a sequence, or a class that provides its own subscript operator. When it is a single scalar value instead - one int, one double, one char, one enum value, a null pointer, or one int returned by a function - compilation stops with "invalid types ... for array subscript", and the two types inside the quotes name the value's type and the index's type, in that order.
The causes in this guide each reproduce the failure with a small, complete program: an int subscripted as if it were a list of counts, a double subscripted as if it were a list of prices, a single char subscripted as if it were a string, an enum value subscripted as if it were a table, a pointer that is still nullptr subscripted as if it pointed at an array, and the int result of a function call subscripted as if the function had returned a container. In each family the failing program is small, the compiler output is shown, and the fix either uses the single value directly or replaces it with the array, pointer, or container the code was reaching for.
Build every program in this guide with g++ -std=c++20 -Wall -Wextra. The failing programs are compiled with -c - compile only - because the error prevents any program from being produced; the fixed programs are compiled and run, so their output is shown.
This guide is about the value before the brackets. Nearby failures that involve brackets but report a different sentence - a class object with no operator[], a floating-point index on a real array, dereferencing an int with * - are at the end in the traps, with the wording each one actually gets, because the repair for those is different. Legal subscripting - arrays, pointers, std::vector, std::string, std::map, and a class that defines operator[] - is there too, because none of it needs fixing.
The rule underneath: built-in subscript needs an array or a pointer, and the index needs to be integral
The built-in subscript operator provides access to an object pointed to by the pointer or array operand. For the built-in operator, one of the two expressions must be a glvalue of type "array of T" or a prvalue of type "pointer to T", while the other expression must be a prvalue of unscoped enumeration or integral type. The built-in expression E1[E2] is exactly identical to *(E1 + E2): the pointer operand is adjusted to point to another element of the same array, following the rules of pointer arithmetic, and is then dereferenced.
Three consequences follow. First, a scalar fails both halves of that rule at once in spirit, but the compiler reports the pair it was given: in count[0], neither side is an array or a pointer, and the message prints the pair as int[int] - the subscripted type first, the index type second. Second, the index 0 does not rescue a scalar, and the common instinct to try value[0] because "the first element" is meant has no element to land on: a single int is not a one-element array, a single char is not a one-character string, and one enum value is not the table of all enumerators. If a one-element sequence is what the code needs, the fix is to declare one - int counts[1], std::string word, an array of enumerators - and subscript that. Third, class types take a different path entirely: a class is subscripted through its own operator[] if it defines one, which is why std::vector, std::string, and std::map are legal, and why a class without one reports "no match for operator[]" instead of this guide's sentence, as the traps show.
An array declaration declares contiguously allocated objects numbered 0 through N - 1, accessed with the subscript operator as in a[0] through a[N - 1]. A pointer can stand in for the array because of array-to-pointer decay: an array expression converts to a pointer to its first element, so a pointer variable holding that address can be subscripted the same way. A nullptr cannot: it is a value of type std::nullptr_t, not a pointer to any element type, so there is no T for the built-in operator to return - the fifth family.
How to read the diagnostic
Every failure in the main families prints the same shape of output: the file and the enclosing function on the first line, then an error line giving the line and column of the subscript, the message "invalid types 'A[B]' for array subscript", and the source line with a caret under the brackets. Read the pair inside the quotes in order. A is the type of the value being subscripted - int, double, char, the enum's name, std::nullptr_t - and it is the side that is wrong in every family in this guide. B is the type of the index - almost always int, because the index written is a whole-number literal. When B is not an integral type, the problem is on the index side instead: a double index (and a pointer index) still prints this guide's sentence with the bad type on the right, as the traps show (int [3][double]), but a scoped-enum index reports "array subscript is not an integer" and a class-type index such as std::string reports "no match for operator[]" instead, so read the sentence itself, not just the pair. The caret is a single character at the [, not under the index alone, which is the visual version of the same point: the compiler is objecting to the combination, and in this guide's families the repair belongs on the left side - use the scalar as a scalar, or make it the sequence it was treated as.
The first failure: an int subscripted as a list
A program that holds one count, and reads it as if it were a list of counts:
int main() {
int count = 5;
return count[0];
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_int.o fail_int.cpp
fail_int.cpp: In function ‘int main()’:
fail_int.cpp:3:17: error: invalid types ‘int[int]’ for array subscript
3 | return count[0];
| ^
count is one int. There is no element 0 inside it, because an int contains a value, not a sequence of values. This shape appears when a variable that used to be an array is changed to a single value and one subscripted use survives the edit, when a name is reused for a scalar after standing for a container earlier in the code, and when code ported from a language where a scalar and a one-element list are interchangeable meets a language where they are different types.
Use the value directly. A scalar needs no index:
#include <iostream>
int main() {
int count = 5;
std::cout << count << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_int fix_int.cpp
$ ./fix_int
5
The output is 5. If the code really will grow to hold several counts, the other repair is to declare the array now - int counts[3] = {5, 0, 0}; - and subscript the array; what does not work is leaving the scalar in place and keeping the brackets, because the brackets are a claim about the type, and the type does not back it.
The second failure: a double subscripted as a list of prices
A program that holds one price, and tries to read the first price out of it:
int main() {
double price = 19.99;
double first = price[0];
return (int)first;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_double.o fail_double.cpp
fail_double.cpp: In function ‘int main()’:
fail_double.cpp:3:25: error: invalid types ‘double[int]’ for array subscript
3 | double first = price[0];
| ^
The pair in the message is double[int]: the subscripted type is double, the index type is int. Floating-point values are scalars in the same way integers are - one double is one number, with no elements inside it. The neighboring mistake, a floating-point index on a real array, reports a pair with the array on the left and double on the right, and is in the traps; here the double is on the left, which is how the two are told apart.
Use the price itself. Naming a copy first does not require an index:
#include <iostream>
int main() {
double price = 19.99;
std::cout << price << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_double fix_double.cpp
$ ./fix_double
19.99
The output is 19.99. When several prices are the real design - one per item, one per day - declare them as a std::vector<double> or an array at the declaration, and every later subscript becomes legal at once; converting at the use site, one subscript at a time, leaves the next use to fail the same way.
The third failure: a char subscripted as a string
A program that holds one character, and tries to read its first character:
int main() {
char grade = 'A';
return grade[0];
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_char.o fail_char.cpp
fail_char.cpp: In function ‘int main()’:
fail_char.cpp:3:17: error: invalid types ‘char[int]’ for array subscript
3 | return grade[0];
| ^
A char is a single character, and a string is a sequence of characters - different types, even though a string's elements are char. The confusion is natural because the two look alike at the keyboard: 'A' with single quotes is one char, while "A" with double quotes is a string holding one character and a terminator. Only the second can be subscripted, because only the second is a sequence. The same applies to a variable declared as char to hold "the initial" or "the grade": the variable holds the whole answer already, and the [0] adds a step that has nowhere to go.
Hold a string when characters will be picked out of it. Here the fix makes the value a std::string, whose subscript returns a reference to the character at the given position:
#include <iostream>
#include <string>
int main() {
std::string word = "Apple";
std::cout << word[0] << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_char fix_char.cpp
$ ./fix_char
A
The output is A - the first character of the string, reached legally because the value in front of the brackets is now a sequence of characters. If the program only ever holds one character, the plainer fix is the first family's: drop the brackets and use the char itself. Choose by what the value means: one character is a char, text is a string, and the type declaration is where that choice belongs.
The fourth failure: an enum value subscripted as a table
A program that holds one color from an enumeration, and subscripts it as if it were the table of colors:
enum class Color { Red, Green, Blue };
int main() {
Color c = Color::Red;
return (int)c[0];
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_enum.o fail_enum.cpp
fail_enum.cpp: In function ‘int main()’:
fail_enum.cpp:4:18: error: invalid types ‘Color[int]’ for array subscript
4 | return (int)c[0];
| ^
The message names the enum type itself, Color, on the left of the pair. An enumerator is one value of that type - Color::Red is a single choice, not the list of choices. The list of choices exists only in the enum definition, as names, and there is no runtime object holding all of them to subscript. Code arrives here when a lookup is designed as "the Nth value of the enum" - the Nth name, the Nth label - and the enum variable is treated as if it were that lookup table.
Build the table as an array, and subscript the array. The enumerators fill it in order:
#include <iostream>
enum class Color { Red, Green, Blue };
int main() {
Color palette[3] = {Color::Red, Color::Green, Color::Blue};
std::cout << (int)palette[0] << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_enum fix_enum.cpp
$ ./fix_enum
0
The output is 0, the underlying value of the first entry, Color::Red. The same pattern holds for a table of names: an array or std::vector of strings in enumerator order, indexed by the converted enum value, is the standard shape - the enum supplies the index, the array supplies the sequence, and neither is asked to be the other.
The fifth failure: a null pointer subscripted as an array
A program that declares a pointer, leaves it null, and subscripts it:
int main() {
auto p = nullptr;
return p[0];
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_nullptr.o fail_nullptr.cpp
fail_nullptr.cpp: In function ‘int main()’:
fail_nullptr.cpp:3:13: error: invalid types ‘std::nullptr_t[int]’ for array subscript
3 | return p[0];
| ^
The left type in the pair is the giveaway: std::nullptr_t is the type of nullptr itself, not a pointer to an element type. auto deduced exactly that type, so p is not an int* waiting to be filled in - it is a null value with no pointed-to type at all, and the built-in subscript has no T to return. This shape appears when a pointer is declared early as a placeholder, with the plan that it will point at an array later, and a subscripted use is written against the placeholder. Even a typed null pointer, int* p = nullptr;, would compile its p[0] and then have undefined behavior at run time; the auto version fails earlier, at compile time, because the type itself carries no element.
Give the pointer something to point at, with the element type stated. An array name converts to a pointer to its first element:
#include <iostream>
int main() {
int values[3] = {10, 20, 30};
int* p = values;
std::cout << p[0] << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_nullptr fix_nullptr.cpp
$ ./fix_nullptr
10
The output is 10, the first element of the array. Pointers may appear as operands to subscript because a pointer to an object represents the address of that object, and pointer arithmetic moves it to neighboring elements of the same array. The declaration order is the lesson: declare the array first, then the pointer that points into it, and a placeholder null pointer never stands where a subscript expects a sequence.
The sixth failure: a function's int result subscripted as a container
A program whose helper returns one number, called as if it returned a list:
int get() {
return 5;
}
int main() {
return get()[0];
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_return.o fail_return.cpp
fail_return.cpp: In function ‘int main()’:
fail_return.cpp:5:17: error: invalid types ‘int[int]’ for array subscript
5 | return get()[0];
| ^
The pair is int[int] again, as in the first family, but the subscripted value is not a variable - it is the prvalue returned by get(). A function returning int returns one number, and that number is a scalar the moment the call completes; the call parentheses do not make it a container. This shape appears when a helper is expected to grow - written to return the first value now, a collection later - and the call site is written for the later version first, and when a call is copied from code whose helper of the same name returns a std::vector.
Make the return type the container the call site expects. Here get() returns a std::vector<int>, whose subscript returns a reference to the element at the given position, with no bounds checking:
#include <iostream>
#include <vector>
std::vector<int> get() {
return {10, 20, 30};
}
int main() {
std::cout << get()[0] << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_return fix_return.cpp
$ ./fix_return
10
The output is 10. The other repair, when the helper will stay scalar, is the first family's again: drop the brackets at the call site and use the returned number. Either way the return type and the call site have to agree about whether the result is one value or many, and the compiler message is the notice that they currently disagree.
The fixes in reach order
- The pair is
int[int],double[int],char[int], or another scalar on the left, and the subscripted value is a variable: the variable is a single value. Use it directly, with no brackets - or, if several values are the real design, change its declaration to an array or astd::vectorand keep the brackets. - The left type is an enum name such as
Color: one enumerator is being treated as the table of enumerators. Build the table as an array of enumerators, names, or values in order, and subscript the array. - The left type is
std::nullptr_t: the pointer was declared withautofromnullptrand has no element type. Declare the array first, then a typed pointer to its first element. - The subscripted value is a function call and the left type is a scalar: the function's return type and the call site disagree. Change the return type to the container, or drop the brackets at the call site.
- The subscripted value is a single
charbut characters will be picked out of it: the value is text, not a character. Declare it as astd::string(or a character array) and keep the brackets. - The wording is anything else - "no match for
operator[]", "invalid type argument of unary*", or a pair whose right side is not an integral type: it is a neighboring problem, not this one. See the traps before changing any declaration.
Traps
- Arrays and pointers are legal, in the same program. An array's elements are numbered
0throughN - 1and accessed with the subscript operator, and a pointer to the first element subscripts the same sequence:
#include <iostream>
int main() {
int a[3] = {10, 20, 30};
int* p = a;
std::cout << a[1] << " " << p[2] << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_legal trap_legal.cpp
$ ./trap_legal
20 30
20 30. Nothing here needs fixing, and the contrast with the first family is the whole rule: a and p name sequences, count named one value.
- The standard containers are legal, each through its own operator[]. A std::vector is subscripted by position, a std::string by position, and a std::map by key - its subscript returns a reference to the value mapped to the key, inserting the key if it does not already exist:#include <iostream>
#include <map>
#include <string>
#include <vector>
int main() {
std::vector<int> v = {10, 20, 30};
std::string s = "hi";
std::map<std::string, int> m;
m["a"] = 7;
std::cout << v[0] << " " << s[1] << " " << m["a"] << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_containers trap_containers.cpp
$ ./trap_containers
10 i 7
The output is 10 i 7. This is why a single char (third family) and a std::string behave differently under the same brackets: the string is a class with a subscript operator, the char is a scalar with none.
- A class of your own is legal once it defines operator[]. The operator is overloadable, and a class that provides it is subscripted through it:
#include <iostream>
struct Row {
int data[3] = {10, 20, 30};
int operator[](int i) const { return data[i]; }
};
int main() {
Row r;
std::cout << r[2] << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_custom trap_custom.cpp
$ ./trap_custom
30
The output is 30. Adding the operator is the right repair when the class really is a sequence-like view over its elements; for a class that merely holds one value, the member name (.v, in the next trap) is the clearer access.
- A class with no operator[] does not produce this guide's sentence. It reports that no overload matches:
struct S {
int v;
};
int main() {
S s{1};
return s[0];
}
$ g++ -std=c++20 -Wall -Wextra -c -o trap_struct.o trap_struct.cpp
trap_struct.cpp: In function ‘int main()’:
trap_struct.cpp:6:13: error: no match for ‘operator[]’ (operand types are ‘S’ and ‘int’)
6 | return s[0];
| ^
The repair is different from every family above: either access the member the code actually wants (s.v), or give the class the subscript operator shown in the previous trap. No declaration change to a scalar is involved, because the subscripted value was never a scalar - it was a class missing an operator.
- Subscripting a pointer to a struct, then naming a member, is legal. The pointer side of the built-in rule is satisfied, and the member access applies to the element the subscript selects:
#include <iostream>
struct S {
int v;
};
int main() {
S items[2] = {{1}, {2}};
S* p = items;
std::cout << p[1].v << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_ptr_struct trap_ptr_struct.cpp
$ ./trap_ptr_struct
2
The output is 2. Compare the previous trap: s[0] on a struct object fails for want of an operator, while p[1] on a pointer to structs succeeds through the built-in operator, because the value before the brackets is a pointer. When struct elements are involved, check whether the value is the object or a pointer to objects before choosing a repair.
- A floating-point index on a real array uses this guide's sentence, but with the fault on the right side of the pair. The array is legal; the index is not an integral type:
int main() {
int a[3] = {1, 2, 3};
return a[1.5];
}
$ g++ -std=c++20 -Wall -Wextra -c -o trap_index.o trap_index.cpp
trap_index.cpp: In function ‘int main()’:
trap_index.cpp:3:13: error: invalid types ‘int [3][double]’ for array subscript
3 | return a[1.5];
| ^
Read the pair before repairing anything: here the left side is int [3], a real array, and the right side is double. The fix is an integral index - a whole-number expression, a loop counter, a converted value - and no declaration changes. In the main families the right side is int and the left side is the scalar, so the declaration is what changes.
- Dereferencing an int with * is the neighboring operator and a different sentence. The built-in indirection operator needs a pointer operand, just as subscript needs an array or pointer, but the wording names the unary operator instead:
int main() {
int x = 5;
return *x;
}
$ g++ -std=c++20 -Wall -Wextra -c -o trap_deref.o trap_deref.cpp
trap_deref.cpp: In function ‘int main()’:
trap_deref.cpp:3:12: error: invalid type argument of unary ‘*’ (have ‘int’)
3 | return *x;
| ^~
Same disease - a scalar treated as if it pointed somewhere - different operator, different sentence, same repair: use the value directly, or make it a pointer to the thing it was treated as pointing at. Searching on one sentence will not find the other's fixes, which is why the exact wording is worth reading before searching.
Checklist
- The pair inside the quotes has been read in order: the subscripted type on the left, the index type on the right.
- For a scalar on the left (
int,double,char, a floating or integral type): the value is used directly with no brackets, or its declaration has been changed to an array or a container and the brackets kept. - For an enum name on the left: a table array has been built in enumerator order, and the array - not the enum value - is subscripted.
- For
std::nullptr_ton the left: a typed pointer now points at a declared array, and no placeholder null pointer stands where a subscript is used. - For a function call: the return type and the call site agree - container return with brackets, scalar return without.
- For a single
charthat is really text: the declaration is now astd::stringor a character array. - The wording has been checked against the traps: "no match for
operator[]" has been treated as a missing operator or a member access, adoubleon the right of the pair as a bad index, and "invalid type argument of unary*" as the dereference problem it is. - A legal shape - an array, a pointer into an array, a
std::vector,std::string, orstd::map, a pointer to structs followed by a member name, a class with its ownoperator[]- has been left alone. - The fixed program run, and its output checked against what the program was meant to do.
Related guides on this site
- Void Value Not Ignored Fix
- 'std::array' Is Not a Member of 'std' Fix
- 'std::unique' Is Not a Member of 'std' Fix
- 'std::search_n' Is Not a Member of 'std' Fix
Sources
- cppreference, Member access operators - for the built-in subscript operator providing access to an object pointed to by the pointer or array operand, one expression needing to be an array or a pointer to
Tand the other an unscoped enumeration or integral type,E1[E2]being identical to*(E1 + E2), and subscript being an overloadable operator. - cppreference, Array declaration - for an array being
Ncontiguously allocated objects numbered0throughN - 1and accessed with the subscript operator, and for array-to-pointer decay constructing a pointer to the first element. - cppreference, Pointer declaration - for a pointer to an object representing the address of the object, and pointers appearing as operands to the built-in indirection operator; pointers as subscript operands are covered by the Member access operators entry above.
- cppreference, std::vector::operator[] - for vector subscript returning a reference to the element at the given position, with no bounds checking performed.
- cppreference, std::basic_string::operator[] - for string subscript returning a reference to the character at the given position.
- cppreference, std::map::operator[] - for map subscript returning a reference to the value mapped to the key, performing an insertion if the key does not already exist.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.