【问题标题】:conflicting types for read_cfg()read_cfg() 的冲突类型
【发布时间】:2017-03-18 05:40:59
【问题描述】:

我有 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;
}

任何帮助都会对我有用

谢谢

【问题讨论】:

  • 请查看问题中的代码:您有 3 种不同的结构类型:struct configstruct datastruct config_data。这些应该是同一类型吗?请阅读minimal reproducible example

标签: c data-structures struct shared-libraries


【解决方案1】:

您对read_cfg() 函数的声明(在example.h 中)与您对read_cfg() 的定义(在examplelib.c 中)不匹配。 将example.h中的声明改为:

extern void read_cfg(struct config_data *cfg);

【讨论】:

    【解决方案2】:

    extern void read_cfg(struct); //错误

    错误是因为您的参数类型不匹配。它应该是void read_cfg(struct config_data *)

    顺便说一句,函数不需要 extern 关键字 - 默认情况下,函数具有外部链接(静态函数除外)。

    【讨论】:

      猜你喜欢
      • 2013-08-04
      • 1970-01-01
      • 2020-07-08
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      相关资源
      最近更新 更多