C和C ++中的空指针之间的差异 [英] Difference between void pointers in C and C++

查看:118
本文介绍了C和C ++中的空指针之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么C ++中的下列错误(但在C中有效)

Why the following is wrong in C++ (But valid in C)

void*p;
char*s;
p=s;
s=p; //this is wrong ,should do s=(char*)p;

为什么需要转换, p 现在包含 char 指针和 s 的地址也是 char 指针?

Why do I need the casting,as p now contains address of char pointer and s is also char pointer?

推荐答案

值不重要,类型是。由于 p 是一个void指针, s 一个字符指针,你必须转换,即使它们具有相同的值。在C中它会确定, void * 是通用指针,但在C ++中是不正确的。

The value doesn't matter, the type does. Since p is a void pointer and s a char pointer, you have to cast, even if they have the same value. In C it will be ok, void* is the generic pointer, but this is incorrect in C++.

p 不包含char指针,它是一个void指针,它包含一个内存地址。

By the way, p doesn't contains char pointer, it's a void pointer and it contains a memory address.

这篇关于C和C ++中的空指针之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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