I have the following part of C code:
我有C代码的以下部分:
char c;
int n = 0;
while ( (c = getchar()) != EOF ){
if (c == "\n"){
n++;
}
}
during compilation, compiler tells me
编译期间,编译器告诉我
warning: comparison between pointer and integer [enabled by default]
The thing is that if to substitute "\n" with '\n' there are no warnings at all Can anyone explain me the reason. Another strange thing that I am not using pointers at all.
问题是,如果用'\n'替换'\n',根本就没有任何警告,谁能给我解释一下原因吗?还有一件奇怪的事,我根本不使用指针。
I am aware of the following questions
我知道以下问题
- warning: comparison between pointer and integer [enabled by default] in c
- 警告:在c中,指针和整型[默认启用]之间的比较
- warning: comparison between pointer and integer in C
- 警告:C中的指针和整数的比较
but in my opinion they are unrelated to my question
但在我看来,它们与我的问题无关
PS. If instead of char c; there will be int c; there will be still warning
PS. If代替char c;有int c;仍然会有警告
2 个解决方案
#1
8
\n'
is called a character literal and is a scalar integer type.
\n'被称为字符文本,是标量整数类型。
"\n"
is called a string literal and is an array type. Note that arrays decay to pointers and so that's why you're getting that error.
“\n”被称为字符串文字,是数组类型。注意,数组衰减到指针,这就是为什么你会得到这个错误。
This may help you understand:
这可能有助于你理解:
// analogous to using '\n'
char c;
int n = 0;
while ( (c = getchar()) != EOF ){
int comparison_value = 10; // 10 is \n in ascii encoding
if (c == comparison_value){
n++;
}
}
// analogous to using "\n"
char c;
int n = 0;
while ( (c = getchar()) != EOF ){
int comparison_value[1] = {10}; // 10 is \n in ascii encoding
if (c == comparison_value){ // error
n++;
}
}
#2
1
Basically '\n' is a literal expression that evaluates to a char. "\n" is a literal expression that evaluates to a pointer. So by using this expression, you are effectively using a pointer.
基本上'\n'是一个文字表达式,计算为一个字符。“\n”是计算指针的文字表达式。通过使用这个表达式,你可以有效地使用指针。
The pointer in question is pointing to a region of memory that contains an array of characters (\n in this case) followed by a termination character that tells code where the array ends.
问题的指针指向一个内存区域,该区域包含一个字符数组(本例中为\n),后面跟着一个终止字符,该字符告诉代码数组的结束位置。
Hope that helps?
希望能帮助吗?
#1
8
\n'
is called a character literal and is a scalar integer type.
\n'被称为字符文本,是标量整数类型。
"\n"
is called a string literal and is an array type. Note that arrays decay to pointers and so that's why you're getting that error.
“\n”被称为字符串文字,是数组类型。注意,数组衰减到指针,这就是为什么你会得到这个错误。
This may help you understand:
这可能有助于你理解:
// analogous to using '\n'
char c;
int n = 0;
while ( (c = getchar()) != EOF ){
int comparison_value = 10; // 10 is \n in ascii encoding
if (c == comparison_value){
n++;
}
}
// analogous to using "\n"
char c;
int n = 0;
while ( (c = getchar()) != EOF ){
int comparison_value[1] = {10}; // 10 is \n in ascii encoding
if (c == comparison_value){ // error
n++;
}
}
#2
1
Basically '\n' is a literal expression that evaluates to a char. "\n" is a literal expression that evaluates to a pointer. So by using this expression, you are effectively using a pointer.
基本上'\n'是一个文字表达式,计算为一个字符。“\n”是计算指针的文字表达式。通过使用这个表达式,你可以有效地使用指针。
The pointer in question is pointing to a region of memory that contains an array of characters (\n in this case) followed by a termination character that tells code where the array ends.
问题的指针指向一个内存区域,该区域包含一个字符数组(本例中为\n),后面跟着一个终止字符,该字符告诉代码数组的结束位置。
Hope that helps?
希望能帮助吗?