24

Ok this has been become sooo confusing to me. I just don't know what is wrong with this assignment:

void *pa; void *pb;
char *ptemp; char *ptemp2; 

ptemp = (char *)pa;
ptemp2 = (char *)pb;

Can anyone tell me why I'm getting this error:

error: invalid conversion from ‘void*’ to ‘char*’

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
Jimmy
  • 887
  • 1
  • 10
  • 24
  • 6
    This code will not produce any errors (assuming the proper context). There's nothing invalid about these conversions. Errors like this are usually generates for `ptemp = pa` assignment in C++ (i.e. no cast, C++ compiler). You are claiming to be using a C compiler and you have an explicit cast there. There won't be such an error in your case. Post real code please. – AnT stands with Russia Aug 15 '11 at 16:50
  • 1
    What compiler? Is it a C compiler, not a C++ compiler? As is, what you have is legal C and will pass through `gcc` without any issues in C99 standards mode. – Yann Ramin Aug 15 '11 at 16:50
  • 1
    Now that's a strange error message... – Karoly Horvath Aug 15 '11 at 16:51
  • The error is probably caused because this assignment statement appears in the global scope rather than in a function. –  Aug 15 '11 at 16:59
  • 2
    @Vlad Lazarenko: That would probably trigger a very different error message. – AnT stands with Russia Aug 15 '11 at 17:02
  • I was using the wrong compiler. I was using g++ instead of gcc. I didn't know that made a difference. – Jimmy Aug 15 '11 at 19:16
  • i know this is 5 years ago, but have you tried to convert it to `char**`? Also, i am wondering what is the purpose of such casting? since i saw it in few places, however the aim wasn't to use the `char` value of the casted pointer, but i think (?) the value of the address itself. if you happen to have a source/explanation, i'll be grateful! – ThunderWiring Nov 24 '16 at 07:30

2 Answers2

32

Actually, there must be something wrong with your compiler(or you haven't told the full story). It is perfectly legal to cast a void* to char*. Furthermore, the conversion is implicit in C (unlike C++), that is, the following should compile as well

 char* pChar;
 void* pVoid;
 pChar = (char*)pVoid; //OK in both C and C++
 pChar = pVoid;        //OK in C, convertion is implicit
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • I'm using the g++ compiler and I've compiled the code on windows visuall c++ with no problem but with g++ i get this error – Jimmy Aug 15 '11 at 19:12
  • Armen, i'm only really bothered about C, but GCC gives me a `-Wincompatible-pointer-types` when passing a `char **` to a function which takes a `void **` as an argument, so is it implicit **only for *single* pointers ?** – A P Jo Sep 25 '20 at 07:29
  • @APJo: Of course, Look a `T*` can be converted to `void*`. If you set `T` as `char*`, you will see that `char**` can be converted implicitly to void*, but not to `void**` – Armen Tsirunyan Sep 25 '20 at 07:53
  • @ArmenTsirunyan I'm not clear. I **need** this to be a `void **` ... – A P Jo Sep 25 '20 at 08:10
  • @APJo: *why* do you need it to be a `void**` and what are you going to do with it? – Armen Tsirunyan Sep 25 '20 at 08:13
  • There's a `int chkd_malloc (void ** assign_to, size_t bytes)` that assigns `*assign_to = NULL` if malloc fails , apart from also returning `-1` . I'm passing `&my_str` which would make it a double-pointer ... At the moment , am just type-casting it with `(void **)` – A P Jo Sep 25 '20 at 08:16
3

I just tried your code in a module called temp.c. I added a function called f1.

void *pa; void *pb;
char *ptemp; char *ptemp2;

f1()
{
        ptemp = (char *)pa;
        ptemp2 = (char *)pb;
}

On Linux I entered gcc -c temp.c, and this compiled with no errors or warnings.

On which OS are you trying this?

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131