我有这两个结构体及其头文件。
我的结构号 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 文件只有 struct
和 typedef
的声明。
typedef struct list * List;
这并没有提供任何关于struct
的成员是什么的线索。它们“隐藏”在 .c 文件中。当编译器编译bal.c
时,它无法知道struct list
有一个成员value
。
您可以通过多种方式解决此问题:
- 将
struct list
的定义也放在.h文件中。 - 提供可以获取/设置
struct list
对象值的函数。
关于C - 头文件中的结构体定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29528746/