简体   繁体   中英

Is it safe to cast void pointer to char pointer pointer

Just wondering if it's safe to cast like this:

char **cpp;
// ... allocation and what not 
void *vp = (void *)cpp;
// ...
cpp = (char **)vp;

should a void ** be used or is void * fine? This works on a couple of my boxes without issue but was wondering if it could cause problems on certain systems.

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

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.

The casting (and usage afterwards) from void* , if the original pointer was char ** .

You shouldn't use void** .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM