函数的类型冲突

时间:2019-08-23 15:34:35

标签: c struct memo

下面的代码是反转字符串中单词的顺序。 但是我得到反向功能的“冲突类型”错误。

我对编译器“预期的'结构字*'给出的错误感到困惑,但是参数的类型为'结构字*'。

我已经在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*);

1 个答案:

答案 0 :(得分:0)

您应该做的是将struct word的声明移到rev的声明之前,这将解决。

我想指出的是为什么您会收到这个模糊的错误,这很难理解:

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

认为编译器从上到下扫描源文件。

它遇到的第一件事是使用一些未知结构rev的函数struct word原型。问题在于它不是结构本身,而是指向该结构的不透明指针。

在C语言中,使用指向以前从未声明过的某种类型的指针是合法的(尽管您会得到警告),只要它只是一个指针并且您从未取消引用它即可。

从编译器的角度来看,任何指针都只有64位(或其他任何位数),仅此而已。指针实际指向什么都没有关系。只要您不尝试取消引用(访问指针所指向的值),编译器就不会真正在意它所指向的数据类型。

编译器的作用是使一些临时类型struct word *仅在函数rev内部有效。 rev可能是来自外部库的函数,或者它具有有关此类型结构的其他知识,例如,它是序列化的数据-在这种情况下,这更有意义,但不是您的情况

接下来,编译将继续,并遇到struct word定义。现在已定义此结构,但从编译器的角度来看,它与函数rev中定义的临时类型不同。

接下来,您调用rev,但是从编译器的角度来看,它的参数struct word *与传递给它的struct word *的类型不同,因此出现了奇怪的错误

相关问题