c字符串返回类型不正确



我的主.c文件中有这两个函数,但当我尝试编译时,我会收到以下错误:

main.c: In function ‘main’:
main.c:30: warning: assignment makes pointer from integer without a cast
main.c: At top level:
main.c:51: error: conflicting types for ‘getFileString’
main.c:30: note: previous implicit declaration of ‘getFileString’ was here

我不明白为什么我不能返回指向我在getFileString方法中创建的字符串的指针。我真的需要解释一下。

我真的不知道为什么会发生这种事,如果能提供任何帮助,我将不胜感激!

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include "tokenizer.h"
int main(int argc, char** argv){
        if(argc != 3){
                printf("not valid # of arguments");
                return 1;
        }
        struct stat info;
        int status;
        status = stat(argv[2], &info);
        if(status != 0){
                printf("Error, errno = %dn", errno);
                return 1;
        }
        //command line argument is file
        if(S_ISREG (info.st_mode)){
                printf("%s is a file n", argv[2]);
                char *string1;
                string1  = getFileString(argv[2]);
                printf("string in file is %s", string1);
                free(string1);
                return 0;
        }
        if(S_ISDIR(info.st_mode)){
                printf("%s is a directory n", argv[2]);
                openDirRec(argv[2]);
                //what to do if command line argument is directory
        }
        return 0;
        /*
           DIR* directory;
           struct dirent* a; 
        //file to write results to 
        FILE *newFile = fopen("results.txt", "w+");
        */
}
char* getFileString(char *fileName){
        FILE* qp;
        qp = fopen(fileName, "r");
        char ch;
        int sizeCheck = 0;
        while((ch=fgetc(qp))!=EOF){
                sizeCheck++;
        }
        fclose(qp);
        if(sizeCheck == 0){
                return NULL;
        }
        else{
                char *fileString;
                fileString = malloc(sizeof(char) * sizeCheck + 1);
                FILE *cp;
                cp = fopen(fileName, "r");
                char cj;
                int count = 0;
                while((cj=fgetc(cp)!=EOF)){
                        fileString[count] = cj;
                        count++;
                }
                fileString[sizeCheck + 1] = '';
                return fileString;
        }
}

在使用函数之前,需要声明函数。因为在声明getFileString()之前使用了它,所以编译器从实际使用的参数推断出参数的类型,并隐式地为其提供返回类型int。然后,当编译器稍后遇到您的定义时,它会注意到返回类型intchar *不匹配,因此会出现错误。

(这也解释了警告main.c:30: warning: assignment makes pointer from integer without a cast——您正在将一个int,或者编译器因为没有告诉它其他情况而决定为int,分配给char *。)

main()函数之前添加此行以声明函数:

char* getFileString(char *fileName);

或者,您可以将整个函数定义移动到main()之前。

(从技术上讲,如果函数之间存在循环依赖关系,您只需要预先声明函数,否则您可以对函数进行排序,这样在定义函数之前就不会使用任何函数。然而,出于代码组织的目的,有时在顶部声明所有函数并稍后实现它们更有意义。)


附带说明一下,始终使用编译器允许的最大警告级别(gcc上的-Wall)进行编译。编译器会发出警告,说它没有看到getFileString()的声明,并且正在生成一个隐式声明。

最新更新