Reputation: 77
I have this C++ code:
#include <stdlib.h>
int main(){
char *Teclas;
Teclas = calloc(1024,sizeof(char));
}
And the compiler is giving the following error:
error: invalid conversion from `void*' to `char*'
What does this error mean and how do I fix it?
Upvotes: 2
Views: 31270
Reputation: 73
If you can make sure there is no other error in your code, you can add g++ flag: -fpermissive
to put the error into warning.
exp: g++ -fpermissive yourcode.cpp
Upvotes: 0
Reputation: 1047
On the android NDK JNI, even with typecasting, old style or new style, the error still doesn't go away.
buffer = (char *) malloc(10);
xxxx=static_cast<char*>(calloc(1024,sizeof(char)));
To make the errors go away, an extra include needs to be added to the path and symbols.
Project -> properties -> C/C++ general -> Path and symbols
On the Includes tab/GNU C++, add the following path (with the appropriate gcc version 4.6, 4.8...) Of course on windows, the path would be be a windows path....
{NDKROOT}/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.6/include
Upvotes: 0
Reputation: 254431
The problem is that you're trying to compile C with a C++ compiler. As the error message says, this line:
Teclas = calloc(1024,sizeof(char));
tries to convert the untyped void*
pointer returned by calloc
into a typed char*
pointer to assign to the variable of that type. Such a conversion is valid in C, but not C++.
The solution is to use a C compiler. It looks like you're using GCC, so just rename the source file to something.c
, and build with gcc
rather than g++
.
If you really must use a compiler for the wrong language, and don't feel like rewriting this in idiomatic C++, then you'll need a cast to force it through the compiler:
Teclas = static_cast<char*>(calloc(1024,sizeof(char)));
or, if you want the code to remain valid C:
Teclas = (char*)calloc(1024,sizeof(char));
But don't do that: use the right compiler for the language. Unless this is the first stage in converting the program to C++; in which case, the next thing to do is get rid of these allocations and use std::string
instead.
Upvotes: 16
Reputation: 4057
The best solution is to just compile your code with a C compiler, as this is C code. You really shouldn't compile C code as if it were C++. However, to directly answer your question, you can force the C++ compiler to compile your C code (which is very bad!!!!).
In C++, whenever you use any function of the alloc
family, you must cast the return value to the type of your lvalue. So, in this case:
Teclas = (char*)calloc(1024, sizeof(char));
The way in which I would do the above is like this:
Teclas = (char*)malloc(1024 * sizeof(*Teclas));
The benefits here: If you change the type of Teclas
, you will still end up with the correct allocation size. Also, I just prefer using malloc
in general.
Additionally, you have major issues throughout your code. For one thing, void int main(...)
???? And you never initialize the contents of Teclas
before you print it, but you free it and calloc it again for some reason. I think you meant to make that a do while loop, but there is no do
.
Also, void KeyLogger();
is WRONG. That is how you declare the function, but since it is a declaration, it should be outside of main.
Upvotes: 0
Reputation:
void int main(int argc,char *argv[])
uhm... perhaps just int main(int argc, char *argv[])
...
Apart from that: this looks like C code. Nothing in these lines suggests that you use C++. The error you are seeing is the result of you treating C code as if it was C++, whereas it isn't, because C is not C++, C++ is not C, and neither is the subset of the other one.
Compile your C code with a C compiler.
Upvotes: 1
Reputation: 794
calloc()
returns a void*. You need to cast its value to whatever type Teclas is, which appears to be a char*
. So Teclas = (char*)calloc(...)
.
Upvotes: 1