简体   繁体   English

是否需要施展无效?

[英]Is Cast to void** needed?

I have the compiler complaining (warning) about the folowing. 我让编译器抱怨(警告)关于下面的内容。

Am I missing something? 我错过了什么吗? Because I thought this didn't need a cast 因为我认为这不需要演员

char* CurrentCh  = some ptr value;
int size;

size = func(&CurrentCh);

with func defined like this 用func这样定义

int func(void** ptr);

Compiler warning: 编译器警告:

passing argument 1 of 'func' from incompatible pointer type 从不兼容的指针类型传递'func'的参数1

Thx 谢谢

In C you can pass any pointer type to a function that expects a void*. 在C中,您可以将任何指针类型传递给期望void *的函数。 What it says is "I need a pointer to something, it doesn't matter what it points to". 它所说的是“我需要一个指向某个东西的指针,它指的是什么并不重要”。 Whereas void** says "I need a pointer to a void*, not a pointer to another pointer type". 而void **说“我需要一个指向void *的指针,而不是指向另一个指针类型的指针”。

In C, void * is the generic pointer type. 在C中, void *是通用指针类型。 But void ** is not a generic pointer-to-pointer type! 但是void **不是通用的指向指针类型! If you want to be able to pass a pointer to a pointer in a generic way, you should use void * anyway: 如果您希望能够以通用方式将指针传递给指针,则应该使用void *

#include <stdio.h>

void func(void *ptr)
{
    char **actual = ptr;
    const char *data = *actual;
    printf("%s\n", data);
}

int main(void)
{
    char *test = "Hello, world";
    func(&test);
    return 0;
}

The cast is necessary as what you do is a form of type punning: You reinterpret the memory which is pointed to from char * to void * . 演员是必要的,因为你所做的是一种形式的双关语:你重新解释从char *指向void *的内存。

For these types, the C standard guarantees that this actually works as char * and void * have the same representation. 对于这些类型,C标准保证这实际上作为char *void *具有相同的表示。 For other type combinations, this may not be the case. 对于其他类型组合,情况可能并非如此。

The relevant parts of the standard are section 6.2.5, §27 标准的相关部分是第6.2.5节,第27节

A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. 指向void的指针应具有与指向字符类型的指针相同的表示和对齐要求。 Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. 同样,指向兼容类型的限定或非限定版本的指针应具有相同的表示和对齐要求。 All pointers to structure types shall have the same representation and alignment requirements as each other. 所有指向结构类型的指针都应具有相同的表示和对齐要求。 All pointers to union types shall have the same representation and alignment requirements as each other. 所有指向union类型的指针都应具有相同的表示和对齐要求。 Pointers to other types need not have the same representation or alignment requirements. 指向其他类型的指针不需要具有相同的表示或对齐要求。

and less relevant (but perhaps also interesting) section 6.3.2.3, §7 第6.3.2.3节,第7节中不太相关(但也许也很有趣)

A pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type. 指向对象或不完整类型的指针可以转换为指向不同对象或不完整类型的指针。 If the resulting pointer is not correctly aligned for the pointed-to type, the behavior is undefined. 如果生成的指针未针对指向类型正确对齐,则行为未定义。 Otherwise, when converted back again, the result shall compare equal to the original pointer. 否则,当再次转换回来时,结果将等于原始指针。 When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object. 当指向对象的指针转换为指向字符类型的指针时,结果指向对象的最低寻址字节。 Successive increments of the result, up to the size of the object, yield pointers to the remaining bytes of the object. 结果的连续增量(直到对象的大小)产生指向对象的剩余字节的指针。

Anything beyond that is implementation-specific. 除此之外的任何事情都是特定于实现的。

In C, any pointer can downcast to void*, but not to void**. 在C中,任何指针都可以向下转换为void *,但不能为void **。 You will need an explicit cast. 你需要一个明确的演员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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