我有2个.c文件,在其中一个文件中我将尝试调用read_cfg(struct)来分配结构中的数据但是我收到.h文件中“冲突类型”的错误
example.c
#include<stdio.h>
#include"example.h"
struct config /structure
{
char data[10];
};
int main()
{
int n=0;
struct data d;
read_cfg(&d); //function call
}
example.h文件
#ifndef EXAMPLE_H
#define EXAMPLE_H
extern void read_cfg(struct); //ERROR
examplelib.c
struct config //structure
{
char data[10];
};
void read_cfg(struct config_data *cfg) //function implementation
{
struct config_data tmp;
strcpy(tmp.data,"helo");
cfg=&tmp;
}
任何帮助对我都有用
谢谢
答案 0 :(得分:1)
extern void read_cfg(struct); //错误
错误是因为您的参数类型不匹配。它应该是void read_cfg(struct config_data *)
。
顺便说一下,函数不需要extern
关键字 - 默认情况下,函数有外部链接(静态函数除外)。
答案 1 :(得分:0)
您对read_cfg()
函数的声明(example.h
)和read_cfg()
(examplelib.c
中)的定义不匹配。
将example.h
中的声明更改为:
extern void read_cfg(struct config_data *cfg);