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

I want to check if user input contains only numbers or not. So, I am using 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 a warning?

+4
source share
3 answers

NULL does not match the null terminator character. Instead, use '\0' .

+14
source

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

+3
source

You can use this to compare characters (or just 0 for that matter);

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

Source: https://habr.com/ru/post/1385395/


All Articles