gcc警告的未知原因:赋值使得指针来自整数而不进行强制转换

时间:2013-05-10 05:48:26

标签: c gcc warnings

这应该被编译成静态库用于作业。当我使用命令:“gcc -c innoprompt.c inprompt.c”时,我收到警告并指向行“fin = openFilePrompt();”来自innoprompt.c。我看不出这会引起这个警告。此外,当我编译链接此库的实际程序时,我得到相同的警告。由于赋值的性质,我不允许更改从库中调用函数的文件。

这是我的文件标题。

#pragma once

#ifndef LIBINFILEUTIL_H
#define LIBINFILEUTIL_H

#include <stdio.h>

FILE* openInputFile(char* fileName);
FILE* openInputFile();

#endif

这是我的inprompt.c     #include“libinfileutil.h”

FILE* openFilePrompt(){
    char fileName[100];
    FILE* fin = NULL;
    do{
        printf("\nPlease enter file to be opened: ");
        fscanf(stdin,"%s",fileName);
        while(fgetc(stdin) !='\n');
        fin = fopen(fileName, "r");
        if(fin==NULL)
            printf("Failed to open file. Please try another file name.\n");
    }while(fin==NULL);
    return fin;
}

最后,这是我的无辜。

#include "libinfileutil.h"

FILE* openInputFile(char* fileName){
    FILE* fin = NULL;
    fin = fopen(fileName, "r");
        if(fin==NULL)
            fin = openFilePrompt();
    return fin;
}

1 个答案:

答案 0 :(得分:4)

标题没有声明函数openFilePrompt(),那么编译器如何知道它返回指针而不是int

按照目前的情况,你的标题声明了两次相同的函数,一次是原型,一次是没有。也许你应该用第二个替换第二个:

FILE *openFilePrompt(void);

然后我也在函数定义中使用void来实现对称性。 请注意,在C语言(与C ++相反)中,声明与:

之间存在很大差异
FILE *openFilePrompt();

这表示函数openFilePrompt()存在,它返回FILE *,但它需要任意(但固定)数量的未指定类型的参数&#39;。

相关问题