I have a bit stupid question about program in C. My compiler says me: warning: comparison between pointer and integer. I really don't know why. I only want to write each char on the screen.
My code:
int i = 0;
char str[50] = {'s', 'a', 'm', 'p','l','e'}; //only for test
while (str[i] != NULL) {
putchar(str[i]);
i++;
}
Can you help me please? I didn't find any usefull answer on the internet.
NULL
is a pointer and str[i]
is the i-th char of the str array. char is and integer type, and as you compare them you get the warning.
I guess you want to check for end of string, that would you do with a check for the char with the value 0 (end of string), that is '\0'
.
BUT: this wont help you as you define it just as and array of chars and not as a string, and you didnt define the termininating 0 in the char array (you get just lucky that it is implicit there).
PS: Next time you should give at least the information where the compiler is complaining.
NULL should only be used in pointer contexts, but here you're comparing it to a character.
You'd normally want something like:
while (str[i] != '\0') {
[or, of course, something like puts(str);
or printf("%s", str);
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With