簡體   English   中英

函數的沖突類型

[英]conflicting types for an function

下面的代碼是顛倒字符串中單詞的順序。 但是我收到反向函數的“沖突類型”錯誤。

我對編譯器給出的錯誤感到困惑“預期的‘struct word *’,但參數的類型為‘struct word *’。

我已經在 main 函數之前完成了 rev 函數的聲明。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char* rev(char*,struct word*);
int countWord(char*);

struct word{
    char word[20];
};

int main(){

    char str[100];
    char* strp = str;
    char result[100];
    printf("\nEnter string: ");
    fgets(strp,100,stdin);

    int noWords = countWord(strp);

    struct word *ptr; 
    ptr = (struct word*)calloc(noWords,sizeof(struct word));
    

    strcpy(result,rev(strp,ptr));
    printf("reverse is: %s",result);

    return 0;
}

int countWord(char* str){

    int count=0;
    char str1[100];
    strcpy(str1,str);
    int i=0;
    while(str1[i]!='\0'){
        if(str1[i]==' ' && str1[i+1]!=' '){
            count++;
        }
    }
    count+=1;
    return count;
}

char* rev(char* strp,struct word *ptr){

    char str[100];
    strcpy(str,strp);
    char temp[20];
    int i=0,j=0,k=0,l=0;

    while(str[i]!='\0'){
        j=0;
        while(str[i]!=' ' && str[i]!='\0'){
            temp[j]=str[i];
            i++;j++;
        }
        if(str[i]==' ')
            i++;
        temp[j]='\0';

        strcpy(ptr[k].word,temp);
        k++;
    }

    char* ret = (char*)malloc(strlen(str)+1);
    //ret[l]='\0';
    k--;

    while(k){
        strcat(ret,ptr[k].word);
        strcat(ret," ");
        k--;
    }

    return (char*)ret;
}

預期結果是具有相反單詞順序的字符串。

Errors and warnings by compiler-
wordRev.c:5:24: warning: ‘struct word’ declared inside parameter list will not be visible outside of this definition or declaration
 char* rev(char*,struct word*);
                        ^~~~
wordRev.c: In function ‘main’:
wordRev.c:26:25: warning: passing argument 2 of ‘rev’ from incompatible pointer type [-Wincompatible-pointer-types]
  strcpy(result,rev(strp,ptr));
                         ^~~
wordRev.c:5:7: note: expected ‘struct word *’ but argument is of type ‘struct word *’
 char* rev(char*,struct word*);
       ^~~
wordRev.c: At top level:
wordRev.c:47:7: error: conflicting types for ‘rev’
 char* rev(char* strp,struct word *ptr){
       ^~~
wordRev.c:5:7: note: previous declaration of ‘rev’ was here
 char* rev(char*,struct word*);

你應該做的是將struct word的聲明移動到rev的聲明之前,這樣就解決了。

不過,我想指出的是,為什么您會收到這個模糊的錯誤,這很難理解:

expected ‘struct word *’ but argument is of type ‘struct word *’

認為編譯器從上到下掃描源文件。

它遇到的第一件事是函數rev原型,它使用了一些未知的 struct struct word 問題在於它不是結構本身,而是指向結構的不透明指針。

在 C 中,使用指向以前從未聲明過的某種類型的指針是合法的(盡管這是一種不好的做法,並且您會收到警告),只要它只是一個指針並且您永遠不會取消引用它。

您會看到,從編譯器的角度來看,任何指針都只是 64(或其他)位數,僅此而已。 指針實際指向什么並不重要。 只要您不嘗試取消引用它(訪問指針指向的值) - 編譯器並不真正關心它指向什么類型的數據。

編譯器所做的是制作一些臨時類型的struct word * ,它只能在函數 rev 中使用。 rev可能是來自外部庫的函數,或者它有一些關於這種類型結構的其他知識,例如它是序列化數據 - 在這種情況下這更有意義,但不是你的情況。

接下來繼續編譯,遇到struct word定義。 現在這個結構體已定義,但從編譯器的角度來看,它與函數rev中定義的臨時類型不同。

接下來調用rev ,但如前所述,從編譯器的角度來看,它的參數struct word *與傳遞給它的struct word *類型不同,因此出現了這個奇怪的錯誤。

暫無
暫無

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

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