C – 函数的错误冲突类型
我是C的新手。我正在尝试从用户那里获取大量文本并计算单词,字符,行,空格和字母的数量。 这就是我所做的:
#include #include int main(void) { char c = getchar(); char previousc; int charcount = 0; int wordcount = 0; int whitespacecount = 0; int linecount = 0; int lettercount = 0; while(c != EOF) { if(isLetter(c) == 1) lettercount++; if(isWhitespace(c) == 1) { whitespacecount++; if(isWhitespace(previousc) == 0) wordcount++; } if(c == "\n") linecount++; previousc = c; c = getchar(); charcount++; } printf("Character Count: %d\n Word Count: %d\n Whitespace Count: %d\n Letter Count: %d\n Line Count: %d\n", charcount, wordcount, whitespacecount, linecount, lettercount); } int isLetter(char c) // 1 for true, 0 for false. { // instead of writing tons of if's if(isalpha(c) > 0) return 1; return 0; } int isWhitespace(char c) // 1 for true, 0 for false. { if(c == "\n" || c == " " || c == " ") return 1; return 0; }
但是我收到了很多错误和警告,我只是输了……
program2.c: In function 'main': program2.c:20: warning: comparison between pointer and integer program2.c: At top level: program2.c:28: error: conflicting types for 'isLetter' program2.c:28: note: an argument type that has a default promotion can't match an empty parameter name list declaration program2.c:14: error: previous implicit declaration of 'isLetter' was here program2.c:35: error: conflicting types for 'isWhitespace' program2.c:35: note: an argument type that has a default promotion can't match an empty parameter name list declaration program2.c:15: error: previous implicit declaration of 'isWhitespace' was here program2.c: In function 'isWhitespace': program2.c:36: warning: comparison between pointer and integer program2.c:36: warning: comparison between pointer and integer program2.c:36: warning: comparison between pointer and integer
我搜索了不同的错误,但没有找到解决我的问题的解决方案。
你能帮我一下吗?
谢谢。
对于
program2.c:20:warning:指针和整数之间的比较
更改
if(c == "\n")
至
if(c == '\n')
对于
program2.c:28:错误:’isLetter’的冲突类型
program2.c:28:注意:具有默认促销的参数类型不能与空参数名称列表声明匹配
program2.c:14:错误:’isLetter’的先前隐式声明在这里
program2.c:35:错误:’isWhitespace’的冲突类型
program2.c:35:注意:具有默认促销的参数类型不能与空参数名称列表声明program2.c匹配:15:错误:’isWhitespace’的先前隐式声明在这里
为您的function定义原型。
int isLetter(char c); int isWhitespace(char c);
对于
program2.c:在函数’isWhitespace’中:
program2.c:36:warning:指针和整数之间的比较
program2.c:36:warning:指针和整数之间的比较
program2.c:36:warning:指针和整数之间的比较
更改
if(c == "\n" || c == " " || c == " ") return 1;
至
if(c == '\n' || c == ' ' || c == '\t')
从第一个错误/警告开始,修复它,然后在每次更改后逐个编译。 通常,你会发现,在一行上摆脱错误/警告也会在后续行中删除其他行。
第20行:
if(c == "\n") linecount++;
发出警告
program2.c:20: warning: comparison between pointer and integer
c
是char(在比较之前内部转换为整数); "\n"
是char
的数组[2](在比较之前内部转换为char *
)。
这就是编译器抱怨比较整数和指针的原因。
你需要将c
与一个字符进行比较(两者都将在内部转换为整数)
if(c == '\n') linecount++;
-
在调用它们之前声明以下函数(即,在函数
main
之上):-
int isLetter(char c);
-
int isWhitespace(char c);
-
-
function
main
:- 用
int c
替换变量声明char c
int c
- 用
isLetter((char)c)
替换函数调用isLetter(c)
isLetter((char)c)
- 用
isWhitespace((char)c)
替换函数调用isWhitespace(c)
isWhitespace((char)c)
- 将变量赋值
previous = c
替换为previous = (char)c
- 将条件语句
if (c == "\n")
替换为if ((char)c == '\n')
- 用
int c
的原因是函数getchar
返回int
以支持EOF
指标。
-
在函数
isWhitespace
,将条件语句更改为:-
if (c == ' ' || c == '\n' || c == '\r' || c == '\t')
-
EOF
是一个整数值,表示输入结束。 这是一个值,对于任何字符ch
, ch == EOF
总是假的。 因此,您应始终将int
类型的值与EOF
进行比较,而不是char
类型。 它在你的机器上运行,因为char
类型是作为signed char
实现的,但是在char
类型是unsigned char
机器上,这不会。
现在发出警告和错误
-
函数的范围从定义或声明的角度直到程序结束。 在声明之前,您在
main
中调用isLetter
之类的函数。 -
"\n"
是字符串文字,而不是字符。 “是" "
和" "
。 这里的字符串文字计算为指向其第一个元素的指针,并且您将此指针与字符(不同类型)进行比较。 相反,你应该分别与'\n'
,' '
,'\t'
。
你必须在main中使用之前声明函数头,例如:
int isLetter(char c); int main(void){ char c = getchar(); char previousc; int charcount = 0; int wordcount = 0; int whitespacecount = 0; int linecount = 0; int lettercount = 0; while(c != EOF) { if(isLetter(c) == 1) lettercount++; if(isWhitespace(c) == 1) { whitespacecount++; if(isWhitespace(previousc) == 0) wordcount++; } if(c == "\n") linecount++; previousc = c; c = getchar(); charcount++; } printf("Character Count: %d\n Word Count: %d\n Whitespace Count: %d\n Letter Count: %d\n Line Count: %d\n", charcount, wordcount, whitespacecount, linecount, lettercount);}
这将解决冲突类型错误。 但如果要检查字符,还必须将“”更改为“”。