C语言 从不兼容的指针类型初始化 [默认启用] - 有什么问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20818771/
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
Initialization from incompatible pointer type [enabled by default] - whats wrong?
提问by user3139356
I have this warning in my code:
我的代码中有这个警告:
initialization from incompatible pointer type [enabled by default]
从不兼容的指针类型初始化[默认启用]
code is as follows:
代码如下:
/*
* Method return some number :-)
* It is TEST METHOD
*/
int check(char array[]) {
int num = 0; // my number
char **elem_p = array; // test
while (*elem_p) { // it is while
num++;
elem_p++;
}
return num; // my return
}
What is wrong? How can I fix this? Thank you. Test method is not relevant, is a sample.
怎么了?我怎样才能解决这个问题?谢谢你。测试方法不相关,是一个样本。
回答by haccks
What is wrong?
怎么了?
arrayis of type char *(pointer to char) but you are using it to initialize char **(pointer to pointer to char) type variable elem_p.
array是类型char *(指向 的指针char),但您使用它来初始化char **(指向指向 的指针的指针char)类型变量elem_p。
How can I fix this?
我怎样才能解决这个问题?
Make pointer and pointee (object to be pointed) compatible to each other. Declare elem_pas char *;
使指针和指针对象(要指向的对象)相互兼容。声明elem_p为char *;
char *elem_p = array;
回答by haccks
char array[]will be rewritten as char* array. You either need char* array[]or char** array, but there is no difference in a function declaration. You probably intended to only have one asterisk, as haccks points out.
char array[]将被重写为char* array. 您需要char* array[]或char** array,但函数声明没有区别。正如 hackks 指出的那样,您可能打算只使用一个星号。
char* array[]) {
int num = 0; // my number
char **elem_p = array; // test
回答by Thiago Hirai
I think you meant char *elem_p = array;. For more information, see 'Pointers and Arrays' at http://www.cplusplus.com/doc/tutorial/pointers/.
我想你的意思是char *elem_p = array;。有关更多信息,请参阅http://www.cplusplus.com/doc/tutorial/pointers/ 上的“指针和数组” 。

