Warning: comparison between pointer and integer?

Sep 18, 2015 at 4:56am
Hi, I wrote a loop to read from start to end of a char[] in C (see partial code below). I was taught that a null char == '\0' == NULL. However, my gcc compiler gave me a warning: comparison between pointer and integer. I don't understand why I have to use '\0' to replace NULL in the while loop to get rid of the warning. Any help, please?

1
2
3
4
5
6
7
8
  char buf[32];
  sprintf(buf, "%d", 234);

  while (buf[i] != NULL){
        tmp = buf[i] - '0';
        result += tmp;
        i++;
  }  
Last edited on Sep 20, 2015 at 7:24pm
Sep 18, 2015 at 5:01am
You are confusing two separate concepts. NULL is for pointers, whereas '\0' is for characters.
Sep 18, 2015 at 6:38am
Interesting. I never realized that NULL is not the same for pointers as it is for characters ('\0') or for numbers (0).
Sep 18, 2015 at 6:42am
At machine code level and assembly level they are usually always the same, but the whole point of a type system is to prevent you from mixing types accidentally ;)
Topic archived. No new replies allowed.