指针和整数警告之间的比较

Car*_*lll 1 c

当我尝试比较指针数组(最初为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]);
    }
}
Run Code Online (Sandbox Code Playgroud)

我经常收到警告:

指针和整数之间的比较

必须采取哪些措施才能删除此警告?如果我将其修改为:

if(*list[0] != input[0])
Run Code Online (Sandbox Code Playgroud)

警告被删除,但程序崩溃了.感谢您的帮助.

hmj*_*mjd 5

类型input[0]是a,char而类型list[0]是a char*.如果你想比较字符串使用strcmp().

但是,这malloc()是不正确的,list内容是未初始化的.我认为,根据其名称和类型,list我们打算列出以下内容char*:

/* No need to cast return value of malloc(). */
char **list = malloc(20 * sizeof(char*));
Run Code Online (Sandbox Code Playgroud)

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

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

/* Compare to input. */
if (0 == strcmp(list[0], input))
{
    /* Strings equal. */
}
Run Code Online (Sandbox Code Playgroud)