【问题标题】:c string return type incorrect?c字符串返回类型不正确?
【发布时间】:2014-12-12 12:58:49
【问题描述】:

我的 main.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 = %d\n", 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] = '\0';
                return fileString;
        }
}

【问题讨论】:

    标签: c string return


    【解决方案1】:

    您需要在使用函数之前声明它们。因为你在声明它之前使用了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() 的声明,并且它正在生成一个隐含的声明。

    【讨论】:

    • 谢谢!我在 c 方面还是个菜鸟,但现在它工作得很好。
    • 实际上我还有另一个小问题,当字符串在命令行中打印出来时,我得到一堆奇怪的字符,看起来像盒子里的十六进制数字,知道这可能是什么吗?
    • @atg963 通常这意味着您正在尝试将某些内容显示为字符串,但它尚未初始化,因此它包含垃圾。
    猜你喜欢
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 2012-07-06
    • 2014-11-10
    • 2014-06-14
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    相关资源
    最近更新 更多