你如何转换无效字符指针的指针用C [英] How do you convert void pointer to char pointer in C

查看:166
本文介绍了你如何转换无效字符指针的指针用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定这已成为混乱的sooo我。我只是不知道什么是错的这个任务:

 无效* PA;无效* PB;
字符* PTEMP;字符* ptemp2;PTEMP =(字符*)PA;
ptemp2 =(字符*)PB;

谁能告诉我为什么我得到这个错误:


  

错误:从'无效*'到'字符*

无效的转换

解决方案

其实,有必须与你的编译器出了问题(或者你还没有告诉完整的故事)。这是完全合法的投无效* 的char * 。此外,转换 在C(与C ++),也就是说,以下应编译以及

 的char * PCHAR;
 无效* PVOID;
 PCHAR =(字符*)PVOID; //在C和C ++ OK
 PCHAR = PVOID; //在C OK,皈依是隐式

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*’

解决方案

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

这篇关于你如何转换无效字符指针的指针用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆