How do you convert void pointer to char pointer in C

前端 未结 2 400
北恋
北恋 2020-12-31 04:10

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 =         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 04:48

    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
    

提交回复
热议问题