用C的指针和整数的比较 [英] Comparison between pointer and integer in C
问题描述
我对自己在C.程序有点愚蠢的问题,我的编译器说我:
的警告:指针和整数的比较。我真的不知道为什么。
我只想写在屏幕上的每个字符。
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.
我的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
是一个指针和海峡[I]
是STR阵列的第i个字符。 char是和整数类型,因为你比较他们,你得到警告。
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.
我猜你要检查字符串的结尾,那你会为值为0(字符串结尾)的字符检查做的,那就是'\\ 0'
。
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'
.
但是:这不会帮助你,你定义它,就像和字符数组,而不是作为一个字符串,你没有定义字符数组中的termininating 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:接下来的时间,你至少应该给那里的编译器是抱怨的信息
PS: Next time you should give at least the information where the compiler is complaining.
这篇关于用C的指针和整数的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!