error: dereferencing pointer to incomplete type in main file
error: dereferencing pointer to incomplete type in main file
您能解释一下为什么 struct Test 不完整以及如何消除错误吗?该错误与 test.h 中的声明或 test.c 中的定义有关吗?我试图将定义代码移动到头文件,但 createTest 不知道类型测试,或者如果我将函数移动到头文件,则会出现错误 multiple definition of createTest
test.h
typedef struct STest Test;
test.c
typedef struct STest {
int v;
char *str;
}Test;
Test *createTest(int v,char *str) {
Test *t=(Test*)malloc(sizeof(Test));
t->v=v; // error
t->str=str; // error
return t;
}
主要功能(main.c)
错误:
main.c|44|错误:解引用指向不完整类型的指针
放
typedef struct STest {
int v;
char *str;
} Test;
进入test.h
.
typedef struct STest Test
只是说Test
是struct STest
的别称。目前,main.c
只知道这些。特别是,main.c
不知道该结构有哪些成员。这对我来说听起来很不完整。
如果您不在头文件中定义结构,那么您的 main.c
.
中将不会 可见
您需要执行以下操作
要点 1.将结构定义放在test.h
头文件中。也使用 包含保护 。
#ifndef __MY_HDR_H_
#define __MY_HDR_H_
typedef struct STest {
int v;
char *str;
}Test;
#endif //__MY_HDR_H_
[编辑:此外,您需要在 .h
文件中添加 createTest()
的函数原型]
要点 2. 在 test.c
和 main.c
中包含 test.h
。
要点3.编译使用
gcc main.c test.c -o output
标准警告:请 do not cast malloc()
和家人的 return 值。
将代码放在头文件中。
typedef struct STest {
int v;
char *str;
} Test;
因为编译器不知道取消引用它。
提示:然后不要转换 malloc 的结果。
你好像定义了两次Test
。
在test.h你做
typedef struct STest Test;
在 test.c 中删除 typedef
并执行:
struct STest {
int v;
char *str;
};
下面是一个完整的例子:
要定义不透明类型,即只有实现其功能的翻译单元才能详细了解的类型,您可以采用以下方法:
opaque.h
#ifndef OPAQUE_H
#define OPAQUE_H
typedef struct S T;
T * createT(int, char *);
#endif
opaque.c
include <stdlib.h>
#include "opaque.h"
struct S
{
int i;
char * p;
};
T * createT(int i, char * p)
{
T * t = malloc(sizeof *t);
if (NULL != t)
{
t->i = i;
t->p = p;
}
return t;
}
并按如下方式使用它:
#include "opaque.h"
int main(void)
{
T * t = createT(0, NULL);
}
您能解释一下为什么 struct Test 不完整以及如何消除错误吗?该错误与 test.h 中的声明或 test.c 中的定义有关吗?我试图将定义代码移动到头文件,但 createTest 不知道类型测试,或者如果我将函数移动到头文件,则会出现错误 multiple definition of createTest
test.h
typedef struct STest Test;
test.c
typedef struct STest {
int v;
char *str;
}Test;
Test *createTest(int v,char *str) {
Test *t=(Test*)malloc(sizeof(Test));
t->v=v; // error
t->str=str; // error
return t;
}
主要功能(main.c)
错误: main.c|44|错误:解引用指向不完整类型的指针
放
typedef struct STest {
int v;
char *str;
} Test;
进入test.h
.
typedef struct STest Test
只是说Test
是struct STest
的别称。目前,main.c
只知道这些。特别是,main.c
不知道该结构有哪些成员。这对我来说听起来很不完整。
如果您不在头文件中定义结构,那么您的 main.c
.
您需要执行以下操作
要点 1.将结构定义放在test.h
头文件中。也使用 包含保护 。
#ifndef __MY_HDR_H_
#define __MY_HDR_H_
typedef struct STest {
int v;
char *str;
}Test;
#endif //__MY_HDR_H_
[编辑:此外,您需要在 .h
文件中添加 createTest()
的函数原型]
要点 2. 在 test.c
和 main.c
中包含 test.h
。
要点3.编译使用
gcc main.c test.c -o output
标准警告:请 do not cast malloc()
和家人的 return 值。
将代码放在头文件中。
typedef struct STest {
int v;
char *str;
} Test;
因为编译器不知道取消引用它。
提示:然后不要转换 malloc 的结果。
你好像定义了两次Test
。
在test.h你做
typedef struct STest Test;
在 test.c 中删除 typedef
并执行:
struct STest {
int v;
char *str;
};
下面是一个完整的例子:
要定义不透明类型,即只有实现其功能的翻译单元才能详细了解的类型,您可以采用以下方法:
opaque.h
#ifndef OPAQUE_H
#define OPAQUE_H
typedef struct S T;
T * createT(int, char *);
#endif
opaque.c
include <stdlib.h>
#include "opaque.h"
struct S
{
int i;
char * p;
};
T * createT(int i, char * p)
{
T * t = malloc(sizeof *t);
if (NULL != t)
{
t->i = i;
t->p = p;
}
return t;
}
并按如下方式使用它:
#include "opaque.h"
int main(void)
{
T * t = createT(0, NULL);
}