簡體   English   中英

C - 函數的錯誤沖突類型

[英]C - error conflicting types for function

我是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;
}

但是我收到了很多錯誤和警告,我只是丟了......

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'的先前隱式聲明在這里

為您的功能定義原型。

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);

  • 在功能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 ((char)c == '\\n')替換條件語句if (c == "\\n") if ((char)c == '\\n')

int c的原因是函數getchar返回int以支持EOF指標。


  • 在函數isWhitespace ,將條件語句更改為:

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

EOF是一個整數值,表示輸入結束。 這是一個值,對於任何字符chch == EOF總是假的。 因此,您應始終將int類型的值與EOF進行比較,而不是char類型。 它正在你的機器上工作,因為char類型是作為signed char實現的,但是在char類型是unsigned char機器上,這不會。

現在來警告和錯誤

  1. 函數的范圍從定義或聲明的角度直到程序結束。 在聲明之前,您在main中調用isLetter之類的函數。

  2. "\\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);}

這將修復沖突類型錯誤。 但如果要檢查字符,你還必須將“”更改為“”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM