C - 头文件中的结构体定义

标签 c gcc compiler-errors opaque-pointers

我有这两个结构体及其头文件。

我的结构号 1 是:

头文件'list.h':

typedef struct list * List;

源文件“list.c”:

struct list {
    unsigned length;
    char * value;
};

我的结构号 2 是:

头文件“bal.h”:

typedef enum {START, END, MIDDLE, COMMENTS, CONDITIONS} TypeListBal;
typedef struct bal * Bal;

源文件“bal.c”:

i've include those header files : 
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "list.h"

struct bal {
    TypeListBal type;
    List name;              // Name of the bal
    List attributes[];          // Array of type struct List
};

当我尝试使用 bal.c 文件中的函数时,例如:

Bal createBal(List name){

char * search;
const char toFind = '=';
    search = strchr(name->value, toFind);

}

我在这一行遇到错误:search = strchr(name->value, toFind); 说:错误:取消引用指向不完整类型的指针

我不知道为什么,因为我已经将 list.h 包含在我的 bal.c 中

我在 Stackoverflow 上读到了很多内容,说这种编程类型被称为:不透明类型 这似乎非常好,但我不知道如何将其他头文件使用到其他源文件中。 我认为我所要做的就是将 list.h 包含在我的 bal.c 文件中。

我正在使用以下命令在 gcc 中进行编译:gcc -g -W -Wall -c bal.c bal.h

非常感谢你!

最佳答案

.h 文件只有 structtypedef 的声明。

typedef struct list * List;

这并没有提供任何关于struct的成员是什么的线索。它们“隐藏”在 .c 文件中。当编译器编译bal.c时,它无法知道struct list有一个成员value

您可以通过多种方式解决此问题:

  1. struct list的定义也放在.h文件中。
  2. 提供可以获取/设置struct list对象值的函数。

关于C - 头文件中的结构体定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29528746/

相关文章:

java - 为什么C、C++、Java不用一补?

c - ONP 中的段错误

linux - 如何使用 sudo yum install 安装 Fortran -dev 包

c++ - 将 `nullptr` 分配给 `bool` 类型。哪个编译器是正确的?

c# - 可访问性、属性类型不一致

c++ - 单例中的 FunktionPointerArray

调用 pthread_create 错误 - 预期主表达式在 'void' 之前

C 编程。比较数组的内容。

c - 当我们将 "cc"添加到 clobber 列表时,程序集输出中会发生什么

c - 使用链表的 C 堆栈中的预期标识符