C++
C++ Invalid Type Argument Unary * Fix
The star asks for the object a pointer points to. When the value after it is not a pointer at all - a count, a price, a single character, a flag - there is no pointed-to object to reach, and compilation stops with 'invalid type argument of unary *'. The value is rarely missing; it is already in hand, and the star is the extra step. This guide separates the cases, reads what the type in the message means, and matches each one to its fix.
Written November 25, 2026 · Tested with GCC 13.3.0 · 8 min read
- The rule underneath: built-in indirection needs a pointer to an object or a function
- How to read the diagnostic
- The first failure: an int dereferenced as an address
- The second failure: a double dereferenced as a pointer to a price
Written November 25, 2026
Tested with GCC 13.3.0
What this guide is
The indirection expression *value asks for the object that value points to. That only makes sense when value is a pointer. When it is a single scalar value instead - one int, one double, one char, one bool, a null pointer value, or one int returned by a function - compilation stops with "invalid type argument of unary *", and the type inside the second pair of quotes names the type that was given instead of a pointer.
The causes in this guide each reproduce the failure with a small, complete program: an int dereferenced as if it were an address, a double dereferenced as if it pointed at a price, a single char dereferenced as if it pointed at a character, a bool dereferenced as if it pointed at a flag, a pointer that is still nullptr in type as well as in value, and the int result of a function call dereferenced as if the function had returned a pointer. In each family the failing program is small, the compiler output is shown, and the fix either uses the single value directly or makes a real pointer to it with & and dereferences that.
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 after the star. Nearby failures that involve * but report a different sentence - an enum value or a struct object, which report "no match for operator*", and a void*, which reports that it is not a pointer-to-object type - are at the end in the traps, with the wording each one actually gets, because the repair for those is different. So is subscripting an int, the neighboring operator from the companion subscript guide. Legal dereferencing - a pointer, a pointer to a struct followed by a member name, an iterator, a std::unique_ptr or std::shared_ptr, and a reference to a pointer - is there too, because none of it needs fixing.
The rule underneath: built-in indirection needs a pointer to an object or a function
The built-in indirection operator provides access to an object or function pointed to by the pointer operand. Its operand must be a pointer to object or a pointer to function, and the result is the lvalue referring to the object or function to which the operand points. A pointer to void cannot be dereferenced, which is why the void* case in the traps gets its own wording instead of this guide's sentence.
Three consequences follow. First, a scalar is not an address: in *x, where x is an int holding 5, the 5 is the value itself, not the location of another value, and there is nothing for the star to follow. Second, taking the address is the step that creates the pointer the star needs: the built-in address-of operator creates a pointer pointing to its operand, so &x has type int*, and *&x is legal - pointers may appear as operands to the built-in indirection operator, which returns the lvalue identifying the pointed-to object, and a pointer to an object represents the address of that object. Third, class types take a different path entirely: a class is dereferenced through its own operator* if it defines one, which is why iterators and smart pointers are legal, and why an enum or a struct with no such operator reports "no match for operator*" instead of this guide's sentence, as the traps show.
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 star, the message "invalid type argument of unary ‘’ (have ‘A’)", and the source line with a caret under the star and its operand. Read A first: it is the type of the value after the star - int, double, char, bool, std::nullptr_t - and it is the side that is wrong in every family in this guide. The caret is ^~ under a one-letter operand - wider when the operand is wider, as the function-call family below shows - spanning the star and its operand, which is the visual version of the same point: the compiler is objecting to the operator applied to this type, not to the value the variable holds. When the wording is anything else - "no match for ‘operator’", "‘void*’ is not a pointer-to-object type", "invalid types ... for array subscript" - the value was not a scalar at all, or a different operator was used, and the repair is a different one; read the sentence itself before changing any declaration, and see the traps for those wordings.
The first failure: an int dereferenced as an address
A program that holds one count, and dereferences it as if it were a pointer to the count:
int main() {
int x = 5;
return *x;
}
$ 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:12: error: invalid type argument of unary ‘*’ (have ‘int’)
3 | return *x;
| ^~
x is one int. The 5 inside it is a value, not an address, so there is no pointed-to object for the star to return. This shape appears when a variable that used to be a pointer is changed to a plain value and one dereferenced use survives the edit, when code is ported from a design where a handle or an ID is dereferenced to reach its record, and when a * is written by habit in front of a return value.
Make a pointer with &, and dereference the pointer. The address-of operator creates the pointer the star needs:
#include <iostream>
int main() {
int x = 5;
int* p = &x;
std::cout << *p << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_int fix_int.cpp
$ ./fix_int
5
The output is 5. The plainer repair, when no pointer is actually needed, is to drop the star and use x itself - the value was already in hand. Choose by intent: if later code will reseat the pointer or do arithmetic on it, keep the pointer; if the line only ever wanted the value, the star was the whole bug.
The second failure: a double dereferenced as a pointer to a price
A program that holds one price, and tries to reach the price through it:
int main() {
double d = 1.5;
return (int)*d;
}
$ 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:17: error: invalid type argument of unary ‘*’ (have ‘double’)
3 | return (int)*d;
| ^~
The type in the message is double. Floating-point values are scalars in the same way integers are - one double is one number, not the address of a number. The cast to int around the expression does not help, because the cast is applied after the dereference, and the dereference is what has no pointer to work on.
Take the address first, then dereference. Here the stored value is a price, and the pointer version reads it back:
#include <iostream>
int main() {
double d = 19.99;
double* p = &d;
std::cout << *p << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_double fix_double.cpp
$ ./fix_double
19.99
The output is 19.99. As in the first family, dropping the star and using d directly is the smaller fix when no pointer is needed; converting at the declaration - declaring the pointer where the address is taken - keeps every later dereference legal at once.
The third failure: a char dereferenced as a pointer to a character
A program that holds one character, and dereferences it:
int main() {
char c = 'A';
return *c;
}
$ 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:12: error: invalid type argument of unary ‘*’ (have ‘char’)
3 | return *c;
| ^~
A char is a single character, not a pointer to one - even though a string's characters are reached through pointers and subscripts elsewhere, the 'A' stored here is the character itself. The confusion is natural because the same letter appears in both roles: in char* s = "A";, the variable is a pointer and *s is its first character; in this family the variable is the character, and there is no first character inside it to reach.
Point at the character, then dereference the pointer:
#include <iostream>
int main() {
char c = 'A';
char* p = &c;
std::cout << *p << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_char fix_char.cpp
$ ./fix_char
A
The output is A. If the program only ever holds one character, the plainer fix is the first family's again: drop the star and use the char itself. If characters will be walked one by one, the value is text - a string or a character array - and a pointer into it is the shape that makes the star legal throughout.
The fourth failure: a bool dereferenced as a pointer to a flag
A program that holds one flag, and dereferences it:
int main() {
bool b = true;
return *b;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_bool.o fail_bool.cpp
fail_bool.cpp: In function ‘int main()’:
fail_bool.cpp:3:12: error: invalid type argument of unary ‘*’ (have ‘bool’)
3 | return *b;
| ^~
The type in the message is bool. A flag is a complete answer - true or false - not an address where an answer is stored. This shape appears most often after a refactor where a bool* out-parameter or a pointer returned by a lookup becomes a plain bool, and one call site keeps the star it needed for the pointer version.
Take the address when a pointer is genuinely wanted, and the dereference reads the flag back:
#include <iostream>
int main() {
bool b = true;
bool* p = &b;
std::cout << *p << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_bool fix_bool.cpp
$ ./fix_bool
1
The output is 1, the printed form of true. In most real code the right fix here is the smaller one: a condition or a return that dereferences a bool almost always wants the bool itself, so drop the star; keep the pointer form only where the flag will be written through the pointer or passed on as one.
The fifth failure: a null pointer value dereferenced before it is a pointer
A program that declares a pointer placeholder, leaves it as nullptr, and dereferences it:
int main() {
auto p = nullptr;
return *p;
}
$ 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:12: error: invalid type argument of unary ‘*’ (have ‘std::nullptr_t’)
3 | return *p;
| ^~
The type in the message 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 indirection operator has no object type to return. This shape appears when a pointer is declared early as a placeholder, with the plan that it will point at a value later, and a dereferencing use is written against the placeholder. Even a typed null pointer, int* p = nullptr;, would compile its *p and then have undefined behavior at run time; the auto version fails earlier, at compile time, because the type itself carries no pointee.
Give the pointer something to point at, with the element type stated:
#include <iostream>
int main() {
int value = 7;
int* p = &value;
std::cout << *p << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_nullptr fix_nullptr.cpp
$ ./fix_nullptr
7
The output is 7. The declaration order is the lesson: declare the value first, then the typed pointer that points at it, and a placeholder null value never stands where a dereference expects a pointer to an object.
The sixth failure: a function's int result dereferenced as a pointer
A program whose helper returns one number, called as if it returned a pointer to the number:
int get() {
return 5;
}
int main() {
return *get();
}
$ 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:12: error: invalid type argument of unary ‘*’ (have ‘int’)
5 | return *get();
| ^~~~~~
The type is int again, as in the first family, but the dereferenced 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 pointer. Note the wider caret, ^~~~~~, under the whole *get() - the operand here is a call, not a single name. This shape appears when a helper is expected to grow - written to return the value now, a pointer to it 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 pointer.
Make the return type the pointer the call site expects. Here get() returns an int* to a value with static storage, so the call site's star is legal:
#include <iostream>
int* get() {
static int value = 42;
return &value;
}
int main() {
std::cout << *get() << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_return fix_return.cpp
$ ./fix_return
42
The output is 42. The other repair, when the helper will stay scalar, is the first family's again: drop the star 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 a value or a pointer to one, and the compiler message is the notice that they currently disagree. A helper returning a pointer to a local variable would be a different bug - the pointed-to object must outlive the call, which is why this fix points at a static.
The fixes in reach order
- The type in the message is
int,double,char,bool, or another scalar, and the dereferenced value is a variable: the variable is a single value. Use it directly, with no star - or, if a pointer is the real design, take its address with&at the declaration and dereference the pointer. - The type in the message is
std::nullptr_t: the pointer was declared withautofromnullptrand has no pointee type. Declare the value first, then a typed pointer to it. - The dereferenced value is a function call and the type in the message is a scalar: the function's return type and the call site disagree. Change the return type to a pointer (to an object that outlives the call), or drop the star at the call site.
- The wording is anything else - "no match for
operator*", "‘void*’ is not a pointer-to-object type", or "invalid types ... for array subscript": it is a neighboring problem, not this one. See the traps before changing any declaration.
Traps
- A pointer is legal, including a pointer into an array. A pointer to an object represents the address of the object, and indirection returns the object it points to; pointer arithmetic moves it to neighboring elements first:
#include <iostream>
int main() {
int a[3] = {10, 20, 30};
int* p = a;
std::cout << *p << " " << *(p + 1) << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_ptr trap_ptr.cpp
$ ./trap_ptr
10 20
10 20. Nothing here needs fixing, and the contrast with the first family is the whole rule: p holds an address, x held a value.
- Dereferencing a pointer to a struct, then naming a member, is legal. The member-of-pointer form p->v is exactly (*p).v for built-in types:#include <iostream>
struct S {
int v;
};
int main() {
S s{42};
S* p = &s;
std::cout << (*p).v << " " << p->v << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_struct_ptr trap_struct_ptr.cpp
$ ./trap_struct_ptr
42 42
The output is 42 42. Compare the struct trap below: *s on a struct object fails for want of an operator, while *p on a pointer to a struct succeeds through the built-in operator, because the value after the star is a pointer.
- An iterator is legal, through its own operator*. Dereferencing the beginning of a std::vector reads its first element, and arithmetic on the iterator moves the dereference with it:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {10, 20, 30};
auto it = v.begin();
std::cout << *it << " " << *(it + 2) << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_iter trap_iter.cpp
$ ./trap_iter
10 30
The output is 10 30. An iterator is not a raw pointer, but it provides the dereference operator a raw scalar lacks - which is why generic code can write *it for both pointers and iterators, and why neither shape belongs in this guide's families.
- The smart pointers are legal, each through its own operator*. A std::unique_ptr and a std::shared_ptr both dereference to the object they own:
#include <iostream>
#include <memory>
int main() {
auto up = std::make_unique<int>(11);
auto sp = std::make_shared<int>(22);
std::cout << *up << " " << *sp << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_smart trap_smart.cpp
$ ./trap_smart
11 22
The output is 11 22. This is the class-type path from the rule section working as designed: the star calls the class's operator, so no built-in pointer is needed - and no scalar gets one this way either.
- A reference to a pointer is legal, and reseating it reseats the pointer. The reference is another name for the pointer variable itself, so assigning through it changes what the pointer points at:
#include <iostream>
int main() {
int x = 5;
int y = 9;
int* p = &x;
int*& rp = p;
rp = &y;
std::cout << *p << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_refptr trap_refptr.cpp
$ ./trap_refptr
9
The output is 9, not 5: rp = &y; reseated p itself, so *p now reads y. The star here was never the problem - p was a pointer all along, and the reference only aliased it.
- An enum value does not produce this guide's sentence. Both a scoped and an unscoped enum report that no overload matches, because an enum is not a scalar the built-in operator rejects in those words - it goes through overload resolution for operator* and finds none:
enum class Color { Red, Green, Blue };
int main() {
Color c = Color::Red;
return (int)*c;
}
$ 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:17: error: no match for ‘operator*’ (operand type is ‘Color’)
4 | return (int)*c;
| ^~
The repair is the same in spirit - use the enumerator directly, or convert it with a cast - but a search on this guide's sentence will not find it, and a scoped enum was the shape most likely to be expected in the main families. It is not there, because its wording is this one. - A struct object does not produce this guide's sentence either. It reports the same "no match" wording as the enum:
struct S {
int v;
};
int main() {
S s{1};
return *s;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_struct.o fail_struct.cpp
fail_struct.cpp: In function ‘int main()’:
fail_struct.cpp:6:12: error: no match for ‘operator*’ (operand type is ‘S’)
6 | return *s;
| ^~
The repair is different from every family above: either access the member the code actually wants (s.v), or point at the object (&s) and dereference the pointer, as in the pointer-to-struct trap. No scalar declaration is involved, because the dereferenced value was never a scalar - it was a class missing an operator.
- Dereferencing a void* does not produce this guide's sentence. A pointer to void cannot be dereferenced, and GCC says so in its own words:
int main() {
int x = 5;
void* p = &x;
return *p;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_voidptr.o fail_voidptr.cpp
fail_voidptr.cpp: In function ‘int main()’:
fail_voidptr.cpp:4:12: error: ‘void*’ is not a pointer-to-object type
4 | return *p;
| ^~
Here a pointer was present all along - what is missing is the pointed-to type. The repair is to cast back to the original pointer type first, static_cast<int*>(p), and dereference that; the void* itself carries no type for the star to return.
- Subscripting an int is the neighboring operator and a different sentence. Subscript needs an array or a pointer just as indirection needs a pointer, but the wording names the subscript instead:
int main() {
int x = 5;
return x[0];
}
$ g++ -std=c++20 -Wall -Wextra -c -o trap_sub.o trap_sub.cpp
trap_sub.cpp: In function ‘int main()’:
trap_sub.cpp:3:13: error: invalid types ‘int[int]’ for array subscript
3 | return x[0];
| ^
Same disease - a scalar treated as if it pointed somewhere or held a sequence - different operator, different sentence, same repair: use the value directly, or make it a pointer or a sequence. Searching on one sentence will not find the other's fixes, which is why the exact wording is worth reading before searching.
Checklist
- The type inside "(have ‘...’)" has been read: it is the type of the value after the star, and in this guide's families it is a scalar or
std::nullptr_t. - For a scalar (
int,double,char,bool): the value is used directly with no star, or its address has been taken with&and the resulting pointer is what gets dereferenced. - For
std::nullptr_t: a typed pointer now points at a declared value, and no placeholder null value stands where a dereference is used. - For a function call: the return type and the call site agree - pointer return with a star, scalar return without - and any pointer returned points at an object that outlives the call.
- The wording has been checked against the traps: "no match for
operator*" has been treated as an enum or a class missing an operator, "‘void*’ is not a pointer-to-object type" as a cast-back-first problem, and "invalid types ... for array subscript" as the subscript problem it is. - A legal shape - a pointer, a pointer to a struct followed by a member name, an iterator, a
std::unique_ptrorstd::shared_ptr, a reference to a pointer - 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
- Invalid Use of Incomplete Type Fix
- Cannot Declare Variable of Abstract Type Fix
- C++ Invalid Types Array Subscript Fix
- C++ Invalid Operands Binary Operator Fix
Sources
- cppreference, Member access operators - for the built-in indirection operator providing access to an object or function pointed to by the pointer operand, its operand needing to be a pointer to object or a pointer to function, a pointer to void not being dereferenceable, indirection being an overloadable operator, and
E1->E2being equivalent to(*E1).E2for built-in types. - cppreference, Pointer declaration - for a pointer to an object representing the address of the object, pointers appearing as operands to the built-in indirection operator returning the lvalue identifying the pointed-to object, a pointer to object being initializable from the address-of operator, and a pointer to any object type converting to a pointer to void with the reverse conversion requiring a cast.
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.