C语言 C - 函数的错误类型冲突

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22436975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:54:38  来源:igfitidea点击:

C - error conflicting types for function

c

提问by shoham

I'm new to C. I'm trying to get a lot of text from the user and count the number of words, characters, lines, whitespaces and letters. This is what I've done:

我是 C 的新手。我试图从用户那里获取大量文本并计算单词、字符、行、空格和字母的数量。这就是我所做的:

#include <ctype.h>
#include <stdio.h>
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;
}

But I get so many errors and warnings I'm just lost...

但是我收到了很多错误和警告,我只是迷路了......

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

I googled the different errors but didn't find a solution that solves my problem.

我用谷歌搜索了不同的错误,但没有找到解决我问题的解决方案。

Can you help me a bit?

你能帮我一点吗?

Thanks.

谢谢。

采纳答案by haccks

For

为了

program2.c:20: warning: comparison between pointer and integer

program2.c:20: 警告:指针和整数之间的比较

Change

改变

 if(c == "\n")  

to

 if(c == '\n')  

For

为了

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:28: 错误: 'isLetter' 的类型冲突
program2.c:28: 注意: 具有默认提升的参数类型不能匹配空参数名称列表声明
program2.c:14: 错误: 先前隐式'isLetter' 的声明在这里
program2.c:35:错误:'isWhitespace' 的类型冲突
program2.c:35:注意:具有默认提升的参数类型不能匹配空参数名称列表声明 program2.c :15: 错误:'isWhitespace' 的先前隐式声明在这里

Define prototypes for your functions.

为您的函数定义原型。

int isLetter(char c);
int isWhitespace(char c);  

For

为了

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:在函数“isWhitespace”中:
program2.c:36:警告:指针和整数之间的比较
program2.c:36:警告:指针和整数之间的比较
program2.c:36:警告:指针和整数之间的比较

Change

改变

if(c == "\n" || c == " " || c == "      ") return 1;

to

if(c == '\n' || c == ' ' || c == '\t') 

回答by barak manos

  • Declare the following functions beforecalling them (i.e., above function main):

    • int isLetter(char c);
    • int isWhitespace(char c);
  • 调用它们之前声明以下函数(即上面的 function main):

    • int isLetter(char c);
    • int isWhitespace(char c);


  • In function main:

    • Replace the variable-declaration char cwith int c
    • Replace the function-call isLetter(c)with isLetter((char)c)
    • Replace the function-call isWhitespace(c)with isWhitespace((char)c)
    • Replace the variable-assignment previous = cwith previous = (char)c
    • Replace the conditional-statement if (c == "\n")with if ((char)c == '\n')
  • 在功能上main

    • 替换变量声明 char cint c
    • 替换函数调用 isLetter(c)isLetter((char)c)
    • 替换函数调用 isWhitespace(c)isWhitespace((char)c)
    • 更换可变分配 previous = cprevious = (char)c
    • 代替条件语句if (c == "\n")if ((char)c == '\n')

The reason for int c, is that function getcharreturns intin order support the EOFindicator.

, 的原因int c是函数getchar返回int以支持EOF指标。



  • In function isWhitespace, change the conditional-statement to:

    • if (c == ' ' || c == '\n' || c == '\r' || c == '\t')
  • 在 function 中isWhitespace,将条件语句更改为:

    • if (c == ' ' || c == '\n' || c == '\r' || c == '\t')

回答by pmg

Start with the first error/warning, fix it and then work your way down one by one always compiling after each change. Often, you will find, getting rid of an error/warning on a line also gets rid of others in subsequent lines.

从第一个错误/警告开始,修复它,然后在每次更改后始终编译。通常,您会发现,在一行中消除错误/警告也会在后续行中消除其他错误/警告。

Line 20:

第 20 行:

            if(c == "\n") linecount++;

gives the warning

发出警告

program2.c:20: warning: comparison between pointer and integer

cis a char (internally converted to an integer before the comparison); "\n"is an array[2] of char(internally converted to char *before the comparison).
That's why the compiler complains about comparing an integer and a pointer.

c是一个字符(在比较之前内部转换为整数);"\n"是一个数组[2] charchar *在比较之前内部转换为)。
这就是编译器抱怨比较整数和指针的原因。

You need to compare cto a character (both will get internally converted to integers)

您需要与c字符进行比较(两者都会在内部转换为整数)

            if(c == '\n') linecount++;

回答by ajay

EOFis an integer value which indicate the end of input. It's a value such that for any character ch, ch == EOFis always false. Therefore, you should always compare a value of inttype with EOF, not chartype. It's working on your machine because the chartype is implemented as signed charbut on machines where chartype is unsigned char, this won't.

EOF是表示输入结束的整数值。这是一个值,对于任何字符ch,ch == EOF总是假的。因此,您应该始终将int类型的值与EOF而不是char类型进行比较。它在你的机器上工作,因为char类型是实现的,signed char但是在char类型是的机器上unsigned char,这不会。

Now coming to the warnings and errors

现在来到警告和错误

  1. The scope of a function is from the point of its definition or declaration till the end of the program. You are calling the functions like isLetterin mainbefore they have been declared.

  2. "\n"is a string literal, not a character. So are " "and " ". The string literal here evaluates to a pointer to its first element and you are comparing this pointer with a character - a different type. You should, instead, compare with '\n', ' ', '\t'respectively.

  1. 函数的作用域是从它的定义或声明点到程序结束。isLettermain声明之前,您正在调用像in 中的函数。

  2. "\n"是字符串文字,而不是字符。那么," "" "。此处的字符串文字计算为指向其第一个元素的指针,并且您正在将此指针与字符进行比较 - 一种不同的类型。你应该,相反,比较'\n'' ''\t'分别。

回答by Muhammad Korra

you have to declare the function header before using it in the main, for example:

您必须在 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);}

that will fix the conflicting types error. but also you'll have to change " " to ' ' if you are checking on characters.

这将修复冲突类型错误。但如果您正在检查字符,您还必须将“”更改为“ ”。