传递给 void** 而不是 void* 会使编译器抱怨类型,为什么? [英] Passing to void** instead void* makes the compiler complain about types, why?

查看:38
本文介绍了传递给 void** 而不是 void* 会使编译器抱怨类型,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么编译器会警告我在此代码中传递不兼容的指针类型:(在这种情况下,void *void **)(我不知道这是否有什么不同,但我使用的是 gnu99 C 版本)

I don't understand why the compiler warn me about passing an incompatible pointer type in this code: (in this context what are the difference between void * and void **) (I don't know if this make some difference but I am using gnu99 C version)

void someFunc(void ** foo) {
    printf("%s\n", *foo);
}

int main() {

    char * text = "some text";
    someFunc(&text);

    return 0;
}

在这不是

void someFunc(void * foo) { 
    printf("%s\n", foo);
}

int main() {

    char * text = "some text";
    someFunc(text);

    return 0;
}

提前致谢

推荐答案

void * 是一种可以隐式转换为任何对象指针类型或从任何对象指针类型转换的类型.void ** 不是 - 因此虽然您可以将 char * 分配给 void *,但您可以char **void ** 做同样的事情.

void * is a type that is implicitly convertible to and from any object pointer type. void ** isn't - so while you can assign a char * to a void *, you can not do the same with char ** and void **.

原因是它们是不兼容的类型: char ** 指向一个 char *, void ** 指向 void *,因此它们的基本类型不匹配.

The reason is that they are incompatible types: char ** points to a char *, void ** points to a void *, so their base types don't match.

这篇关于传递给 void** 而不是 void* 会使编译器抱怨类型,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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