且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将void指针强制转换为char指针指针是否安全?

更新时间:2022-06-27 00:49:58

强制转换始终是安全的,只要指针有效,则取消引用是安全的. 唯一打算使用void **的情况是,当您打算取消引用以获得void *时.

The cast is always safe, dereferencing it is safe as long as the pointer is valid. The only case where you'd use a void ** is when you plan to dereference it to get a void *.

但是,除非您执行指针算术运算,否则它实际上并不重要.正如您在 http://codepad.org/UcZUA0UL 上看到的那样,无论是否使用void*void **.在实际使用指针之前,无论如何都要将其转换回char **-因此在void -ish时永远不会取消引用它.

However, unless you do pointer arithmetics it won't really matter. As you can see on http://codepad.org/UcZUA0UL it works fine no matter if you use void* or void **. Before you actually use the pointer you are going to cast it back to char ** anyway - so it is never dereferenced while it's void-ish.