简体   繁体   中英

conflicting types for an function

Code below is to reverse the order of words in string. But I am getting 'conflicting types for' error for reverse function.

I am confuse about the error given by compiler 'expected 'struct word *' but argument is of type 'struct word *'.

I have done declaration of rev function before the main function.

#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;
}

Expected result was the string having reverse order of words.

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

What you should do is to move the declaration of struct word before the declaration of of rev, and that would solve.

Something I wanted to point out though is why you are getting that vague error, which is hard to understand:

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

Think that the compiler scans the source file from top to bottom.

First thing it encounters is function rev prototype that uses some yet unknown struct struct word . Problem is that it is not the struct itself but an opaque pointer to the struct.

In C it is legal (although a bad practice and you get a warning about that) to use a pointer to some type that was never declared before, as long as it is only a pointer and you never dereference it.

You see, from compiler point of view, any pointer is just 64 (or whatever) bits number, no more. It does not matter what the pointer actually points to. As long as you are not trying to dereference it (access the value the pointer points to) - compiler does not really care what type of data it points to.

What compiler does is making some temporary type struct word * which valid to use only inside the function rev. It is possible that rev is a function from an external library, or it has some other knowledge about this type structure, for example it is serialized data - in this case this would make more sense, but it is not your case.

Next, compilation continues, and encounters struct word definition. Now this struct is defined, but from the compiler point of view, it is not the same temporary type as was defined in function rev .

Next you invoke rev , but as said, from compiler point of view, its argument struct word * is not the same type as struct word * you pass to it, hence comes this weird error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM