warning: comparison between pointer and integer [enabled by default] in c

橙三吉。 提交于 2019-12-22 08:48:08

问题


I want to check whether the user input contains only digits or not. So, I use the following code:

for(i = 0; argv[1][i] != NULL; i++)
    if(!isdigit(argv[1][i]))
    {
        printf("Error");
        return -1;
    }

It works well but I got this warning:

warning: comparison between pointer and integer [enabled by default]

since argv[1][i] is an Integer and NULL is a pointer. How can I avoid such warning?


回答1:


NULL is not the same as the null-terminator character. You should use '\0' instead.




回答2:


argv[1][i] is a char, just compare it with '\0' or 0, it's clearer.




回答3:


For a character comparison, this can be used (or just 0 for that matter);

for(i = 0; argv[1][i] != '\0'; i++)


来源:https://stackoverflow.com/questions/8435432/warning-comparison-between-pointer-and-integer-enabled-by-default-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!