C++
C++ Invalid Conversion Void* Fix
A void* holds an address without saying what type lives there, so C++ will not hand it to an int* on its own - you have to state the target type yourself. That one missing statement covers several different shapes: a plain variable, a malloc result, a function return value, a function argument, and a return statement. This guide separates those shapes, reads what the diagnostic actually reports, and matches each one to its fix.
Written November 25, 2026 · Tested with GCC 13.3.0 · 8 min read
- The rule underneath: any object pointer converts to void, not back
- How to read the diagnostic
- The first failure: a void variable initializing an int
- The second failure: a std::malloc result assigned to an int
Written November 25, 2026
Tested with GCC 13.3.0
What this guide is
A void* can hold the address of any object, but it carries no information about the type of that object. When code needs an int* - to read or write an int through the pointer - the compiler requires the conversion to be written explicitly. Leave it out, and compilation stops with "invalid conversion from 'void' to 'int'".
The causes in this guide each reproduce the failure with a small, complete program: a void* variable initializing an int*, a std::malloc result assigned to an int* without a cast, a function returning void* whose result is assigned to an int*, a void* passed as an argument to an int* parameter, and a function declared to return int* that returns a void* variable. In each family the failing program is small, the compiler output is shown, and the fix is the same statement in a different place: a static_cast<int*> at the point where the void* becomes an int*.
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.
The rule underneath: any object pointer converts to void*, not back
A pointer to an object of any type can be implicitly converted to a pointer to void; the pointer value is unchanged. The reverse conversion is not implicit: it requires a static_cast or an explicit cast, and it yields the original pointer value.
That asymmetry produces every family below. Going into a void* loses the pointed-to type on purpose - that is what makes void* useful for C interfaces and for functions such as std::malloc, which return storage without knowing what will be stored in it. Coming back out, the compiler cannot check that the type you name is the type that was actually stored, so the language makes you name it. A static_cast from a pointer to void to a pointer to an object type is exactly that naming: it is allowed when the target is an object type, and if the original pointer came from that type, the result has the original value.
Two consequences follow. First, the cast is a promise, not a check: casting a void* that points at a double to int* compiles, and reading through it is wrong. Cast to the type the pointer actually came from. Second, this is a place where C and C++ differ: in C, a void* converts implicitly to other object pointer types, so C code that assigns a malloc result directly compiles there and fails here. Code moved from a C file into a C++ file meets this error first at its malloc calls.
How to read the diagnostic
Every failure in the main families prints the same shape of output. The first family's, in full:
fail_var.cpp: In function ‘int main()’:
fail_var.cpp:5:14: error: invalid conversion from ‘void*’ to ‘int*’ [-fpermissive]
5 | int* p = raw;
| ^~~
| |
| void*
Read it in order. The first line gives the file and the function. The error line names the two types in order - from void*, the type you have, to int*, the type the context requires - and the caret sits under the void* expression in your line, with its type repeated below. The [-fpermissive] tag at the end names the compiler flag that would downgrade this error to a warning; the traps section shows what that flag does, and why it is not the fix. Whatever context required the int* - an initializer, an assignment, an argument, a return - the repair is at that same spot: convert the expression there.
The first failure: a void variable initializing an int
A program that stores an int's address in a void* and initializes an int* from it:
#include <iostream>
int main() {
int value = 42;
void* raw = &value;
int* p = raw;
std::cout << *p << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_var.o fail_var.cpp
fail_var.cpp: In function ‘int main()’:
fail_var.cpp:5:14: error: invalid conversion from ‘void*’ to ‘int*’ [-fpermissive]
5 | int* p = raw;
| ^~~
| |
| void*
The line above the error is legal: &value, an int*, converts implicitly to void*, so raw is fine. Only the step back out fails. Nothing is wrong with the address itself - raw does point at an int - but the compiler will not take that on trust from the type alone.
The fix names the type with static_cast:
#include <iostream>
int main() {
int value = 42;
void* raw = &value;
int* p = static_cast<int*>(raw);
std::cout << *p << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_var fix_var.cpp
$ ./fix_var
42
The output is 42. Because raw came from an int*, the cast back yields that original pointer, and reading through it gives the stored value.
The second failure: a std::malloc result assigned to an int*
A program that assigns the result of std::malloc directly to an int*:
#include <cstdlib>
#include <iostream>
int main() {
int* p = std::malloc(sizeof(int));
*p = 42;
std::cout << *p << '\n';
std::free(p);
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_malloc.o fail_malloc.cpp
fail_malloc.cpp: In function ‘int main()’:
fail_malloc.cpp:4:25: error: invalid conversion from ‘void*’ to ‘int*’ [-fpermissive]
4 | int* p = std::malloc(sizeof(int));
| ~~~~~~~~~~~^~~~~~~~~~~~~
| |
| void*
std::malloc returns void* - it allocates bytes without knowing what type will live in them - so this is the first family with the void* coming from a function instead of a variable. It is also the shape most often carried over from C, where the same line is legal and needs no cast. In C++ the cast is required, and the size expression is unchanged: sizeof(int) was already the right request.
The fix casts the result:
#include <cstdlib>
#include <iostream>
int main() {
int* p = static_cast<int*>(std::malloc(sizeof(int)));
*p = 42;
std::cout << *p << '\n';
std::free(p);
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_malloc fix_malloc.cpp
$ ./fix_malloc
42
The output is 42. The cast does not change the allocation - it states that the bytes are to be used as an int, which is what the following lines do.
The third failure: a function returning void*
A program that assigns the result of a void*-returning function to an int*:
#include <iostream>
void* get_raw(void* input) {
return input;
}
int main() {
int value = 42;
int* p = get_raw(&value);
std::cout << *p << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_func_return.o fail_func_return.cpp
fail_func_return.cpp: In function ‘int main()’:
fail_func_return.cpp:7:21: error: invalid conversion from ‘void*’ to ‘int*’ [-fpermissive]
7 | int* p = get_raw(&value);
| ~~~~~~~^~~~~~~~
| |
| void*
Functions that pass pointers through generic interfaces - C library callbacks and wrappers are the common source - commonly return void*, because the function itself does not know the pointed-to type. The caller does know: here the argument went in as the address of an int. The error is reported at the call in main, not inside get_raw, whose own return of its void* parameter is a void*-to-void* copy and is fine.
The fix casts the call result at the assignment:
#include <iostream>
void* get_raw(void* input) {
return input;
}
int main() {
int value = 42;
int* p = static_cast<int*>(get_raw(&value));
std::cout << *p << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_func_return fix_func_return.cpp
$ ./fix_func_return
42
The output is 42. The function is unchanged; the conversion belongs to the caller, who is the one who knows the type.
The fourth failure: a void passed to an int parameter
A program that passes a void* variable to a function whose parameter is an int*:
#include <iostream>
void show(int* p) {
std::cout << *p << '\n';
}
int main() {
int value = 42;
void* raw = &value;
show(raw);
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_argument.o fail_argument.cpp
fail_argument.cpp: In function ‘int main()’:
fail_argument.cpp:8:10: error: invalid conversion from ‘void*’ to ‘int*’ [-fpermissive]
8 | show(raw);
| ^~~
| |
| void*
fail_argument.cpp:2:16: note: initializing argument 1 of ‘void show(int*)’
2 | void show(int* p) {
| ~~~~~^
Passing an argument is an initialization of the parameter, so the same missing conversion is reported - with one extra line. The note names the context the other families leave you to find: "initializing argument 1" of show(int*), with a pointer to the parameter declaration. When the types in an error look unrelated to the line you are reading, that note is the part that says which parameter required the target type.
The fix casts the argument at the call:
#include <iostream>
void show(int* p) {
std::cout << *p << '\n';
}
int main() {
int value = 42;
void* raw = &value;
show(static_cast<int*>(raw));
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_argument fix_argument.cpp
$ ./fix_argument
42
The output is 42. The function signature is unchanged - the parameter was always an int*; only the argument needed to arrive as one.
The fifth failure: an int function returning a void
A program whose function is declared to return int* and returns a void* variable:
#include <iostream>
int* get_pointer(void* raw) {
return raw;
}
int main() {
int value = 42;
int* p = get_pointer(&value);
std::cout << *p << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_return_stmt.o fail_return_stmt.cpp
fail_return_stmt.cpp: In function ‘int* get_pointer(void*)’:
fail_return_stmt.cpp:3:12: error: invalid conversion from ‘void*’ to ‘int*’ [-fpermissive]
3 | return raw;
| ^~~
| |
| void*
A return statement initializes the function's return value, so it is the same conversion in a third context. Note where the error is reported this time: inside get_pointer, at the return, under its own function heading - not at the call in main, which receives a proper int* once the function itself is fixed. Of the five families, this is the one where the repair and the report are in a different function from the code that uses the result.
The fix casts in the return statement:
#include <iostream>
int* get_pointer(void* raw) {
return static_cast<int*>(raw);
}
int main() {
int value = 42;
int* p = get_pointer(&value);
std::cout << *p << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_return_stmt fix_return_stmt.cpp
$ ./fix_return_stmt
42
The output is 42. Fixing the conversion inside the function fixes every caller at once, which is why this family is repaired at the return rather than at each call site.
The fixes in reach order
- The context is an initializer or an assignment from a
void*variable: wrap the variable instatic_cast<int*>(...)- or instatic_castto whichever pointer type the left-hand side declares. - The
void*is astd::mallocresult: cast the call result, keeping thesizeofexpression as it is. Code newly arrived from C meets the error here first. - The
void*is a function's return value: cast the call result at the assignment; the returning function stays unchanged. - The error carries an "initializing argument" note: cast the argument at the call site, to the parameter type the note names.
- The error is reported at a
returnstatement inside a function: cast there, inside the function, so every caller is fixed at once. - The source type in the error is
const void*, notvoid*: that is a different problem - see the traps. A plainstatic_cast<int*>does not fix it. - In every case: cast to the type the pointer actually came from, recompile, and only then read any errors that remain.
Traps
- A C-style cast also compiles -
(int*)rawdoes the conversion, and this program runs:
#include <iostream>
int main() {
int value = 42;
void* raw = &value;
int* p = (int*)raw;
std::cout << *p << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_c_cast trap_c_cast.cpp
$ ./trap_c_cast
42
42. The C-style cast is legal here, and existing C code ported to C++ often keeps it. static_cast is the narrower statement of the same intent: it performs this conversion and cannot silently become a different kind of cast if the types change later, which is why the fixes in this guide use it.
- The reverse direction needs no cast at all. An int* converts implicitly to void*, so storing a typed pointer in a void* - the first step in most programs in this guide - is never this error:#include <iostream>
int main() {
int value = 42;
int* p = &value;
void* raw = p;
int* back = static_cast<int*>(raw);
std::cout << *back << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_reverse trap_reverse.cpp
$ ./trap_reverse
42
The output is 42. Only the step back out of the void* - the static_cast on the last declaration - has to be written. If a line converting a typed pointer into a void* appears to fail, the types in the error will name something else; read them rather than adding a cast.
- A const void* source changes the wording, and the usual fix fails with it. Assigning a const void* to an int* reports a different from-type:
#include <iostream>
int main() {
int value = 42;
const void* raw = &value;
int* p = raw;
std::cout << *p << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o trap_const_fail.o trap_const_fail.cpp
trap_const_fail.cpp: In function ‘int main()’:
trap_const_fail.cpp:5:14: error: invalid conversion from ‘const void*’ to ‘int*’ [-fpermissive]
5 | int* p = raw;
| ^~~
| |
| const void*
Writing the usual fix against it produces a different error again, because static_cast cannot cast away constness:
#include <iostream>
int main() {
int value = 42;
const void* raw = &value;
int* p = static_cast<int*>(raw);
std::cout << *p << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o trap_const_static_only.o trap_const_static_only.cpp
trap_const_static_only.cpp: In function ‘int main()’:
trap_const_static_only.cpp:5:14: error: ‘static_cast’ from type ‘const void*’ to type ‘int*’ casts away qualifiers
5 | int* p = static_cast<int*>(raw);
| ^~~~~~~~~~~~~~~~~~~~~~
Two repairs work. Keeping the const - the usual case, when the code only reads through the pointer - casts to const int*:
#include <iostream>
int main() {
int value = 42;
const void* raw = &value;
const int* p = static_cast<const int*>(raw);
std::cout << *p << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_const_fix_a trap_const_fix_a.cpp
$ ./trap_const_fix_a
42
The output is 42. When the pointer genuinely has to become writable and the object it points at was not itself declared const - here value is a plain int - removing the const is a separate, explicit step with const_cast, before the static_cast:
#include <iostream>
int main() {
int value = 42;
const void* raw = &value;
int* p = static_cast<int*>(const_cast<void*>(raw));
std::cout << *p << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_const_fix_b trap_const_fix_b.cpp
$ ./trap_const_fix_b
42
The output is 42. Reach for the const int* version first: it keeps the promise the const void* was making, and writing through a pointer obtained by removing const from an object that really is const is undefined behavior, so the second form is only for pointers whose target was never const.
- The [-fpermissive] tag in every error above is not a fix. Compiling the first failing program with that flag downgrades the error to a warning and produces a working program:
$ g++ -std=c++20 -Wall -Wextra -fpermissive -o permissive_var fail_var.cpp
fail_var.cpp: In function ‘int main()’:
fail_var.cpp:5:14: warning: invalid conversion from ‘void*’ to ‘int*’ [-fpermissive]
5 | int* p = raw;
| ^~~
| |
| void*
$ ./permissive_var
42
The output is 42 - and the source still contains the invalid conversion, accepted only by this compiler under this flag. Other compilers reject the same code, and the flag downgrades other unrelated errors along with this one. Add the cast instead; it is one expression, and it makes the code valid everywhere.
Checklist
- Every
void*that becomes a typed pointer passes through astatic_castto that type - at the initializer, the assignment, the argument, or the return where the conversion happens. - Each cast names the type the pointer actually came from, not merely the type the receiving variable declares.
std::mallocresults cast at the assignment, with thesizeofexpression unchanged.- Argument failures fixed at the call site, to the parameter type named in the "initializing argument" note.
- Return-statement failures fixed inside the returning function, not at its callers.
- A
const void*source cast to aconstpointer type - or, only when the target object was neverconst, put throughconst_castas a separate visible step. - No
-fpermissivein the build to make the error go away. - The fixed program run, and its output checked against what the program was meant to do.
Related guides on this site
- Fix C++ "invalid conversion from 'const char*' to 'char*'" — Add const, Copy the Text, or Own It in std::string
- Void Value Not Ignored Fix
- Invalid Use of Incomplete Type Fix
- C++ Invalid Types Array Subscript Fix
Sources
- cppreference, Pointer declaration - for a pointer to an object of any type converting implicitly to a pointer to void, for the reverse conversion requiring static_cast or an explicit cast, and for std::malloc returning void*.
- cppreference, static_cast conversion - for static_cast converting a pointer to void to a pointer to an object type, and for static_cast being unable to cast away constness.
- cppreference, Implicit conversions - for implicit conversions applying when an expression initializes a new object, is passed as a function argument, and appears in a return statement.
- GCC, C++ Dialect Options - for -fpermissive downgrading some diagnostics from errors to warnings.
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.