C++ 从“void*”到“unsigned char*”的无效转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10157252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:40:49  来源:igfitidea点击:

Invalid conversion from ‘void*’ to ‘unsigned char*’

c++void-pointersunsigned-char

提问by jwbensley

I have the following code;

我有以下代码;

void* buffer = operator new(100);
unsigned char* etherhead = buffer;

I'm getting the following error for that line when trying to compile;

尝试编译时,该行出现以下错误;

error: invalid conversion from ‘void*' to ‘unsigned char*'

Why do I get that error, I thought a void was "type-less" so it can point at anything, or anything can point to it?

为什么我会收到这个错误,我认为 void 是“无类型的”,所以它可以指向任何东西,或者任何东西都可以指向它?

回答by josephthomas

You need to cast as you can not convert a void* to anything without casting it first.

您需要进行强制转换,因为如果不先进行转换,就无法将 void* 转换为任何内容。

You would need to do

你需要做

unsigned char* etherhead = (unsigned char*)buffer;

(although you could use a static_castalso)

(虽然你也可以使用 a static_cast

To learn more about void pointers, take a look at 6.13 — Void pointers.

要了解有关空指针的更多信息,请查看6.13 — 空指针



The "type-less" state of void*only exist in C, not C++ with stronger type-safety.

“无类型”状态void*只存在于 C 中,而不存在于具有更强类型安全性的 C++ 中。

回答by CB Bailey

A void*might point at anything and you can convert a pointer to anything else to a void*without a cast but you have to use a static_castto do the reverse.

Avoid*可能指向任何东西,您可以将指向任何其他东西的指针转换为 avoid*而无需强制转换,但您必须使用 astatic_cast来做相反的事情。

unsigned char* etherhead = static_cast<unsigned char*>(buffer);

If you want a dynamically allocated buffer of 100 unsigned charyou are better off doing this and avoiding the cast.

如果您想要动态分配的 100 缓冲区,unsigned char最好这样做并避免强制转换。

unsigned char* p = new unsigned char[100];

回答by mjfgates

You can convert any pointer to a void *, but you can't convert void * to anything else without a cast. It might help to imagine that "void" is the base class for EVERYTHING, and "int" and "char" and whatnot are all subclasses of "void."

您可以将任何指针转换为 void *,但不能在没有强制转换的情况下将 void * 转换为任何其他指针。想象一下“void”是所有东西的基类可能会有所帮助,而“int”和“char”等等都是“void”的子类。

回答by Philipp

Here's a bit of lateral thinking: Whenever you think you need casts or pointers, think again. If all you need is 100 unsigned bytes of memory, use

这里有一点横向思考:每当你认为你需要强制转换或指针时,再想一想。如果您只需要 100 个无符号字节的内存,请使用

std::array<unsigned char, 100> data;

or

或者

unsigned char data[100];

If the size is not constant, use a vector:

如果大小不恒定,请使用向量:

std::vector<unsigned char> data(size);

Raw pointers, the newoperator, and casts are unsafe, hard to get right and make your program harder to understand. Avoid them if possible.

原始指针、new操作符和强制转换是不安全的,难以正确理解并且使您的程序更难理解。如果可能,请避免使用它们。

回答by Spike

C++ is designed to be more type safe than C. If this is C code, it may be OK, but also depends on what compiler you are using right now.

C++ 被设计为比 C 更安全。如果这是 C 代码,它可能没问题,但也取决于您现在使用的编译器。

Also, technically, "extern "C" int *" and "int *" are different types... (like solaris compiler will pick this out)

此外,从技术上讲,"extern "C" int *" 和 "int *" 是不同的类型......(就像solaris编译器会挑选出来)

I would suggest you using C++ style cast instead of C cast. There are more descriptions here:

我建议您使用 C++ 样式转换而不是 C 转换。这里有更多描述:

Regular cast vs. static_cast vs. dynamic_cast.

常规转换 vs. static_cast vs. dynamic_cast

回答by Erwald

void *pt; 

pt=(void*)&i; 
pt=(void*)&dbl; 

Here's how I would do it.

这是我将如何做到的。