1

当我尝试比较指针数组(最初为 NULL)和 char 指针时:

int main(int argc, char **argv){   

    char **list = (char**)malloc(20*sizeof(char)+1);
    char *input = "La li lu le lo";


    if(*list[0] != input[0]) { //or if(list[0][0]!=input[0])
        printf("false: %s", strdict[0]);
    }
}

我经常收到警告:

指针与整数的比较

必须做什么才能消除此警告?如果我将其修改为:

if(*list[0] != input[0])

警告被删除,但程序崩溃。提前感谢您的帮助。

4

2 回答 2

5

of 的类型input[0]是 achar而 of 的类型list[0]是 a char*。如果您想比较字符串,请使用strcmp().

但是malloc()不正确,list内容未初始化。我认为,根据其名称和类型,list旨在成为以下列表char*

/* No need to cast return value of malloc(). */
char **list = malloc(20 * sizeof(char*));

然后每个元素都char*需要设置为 some char*,也可能是malloc()d :

list[0] = malloc(20); 
/* Populate list[0] with some characters. */

/* Compare to input. */
if (0 == strcmp(list[0], input))
{
    /* Strings equal. */
}
于 2012-10-23T15:39:50.493 回答
3

似乎您正在将整数与数组进行比较,因为 List 前面有两颗星。Input[0] 是一个字符,而 List[0] 是一个数组,如果您查看 List[0][0] 那么您将比较两个等效对象。

于 2012-10-23T15:39:08.807 回答