标题“point.h”中有以下代码:
#ifndef POINT_H_INCLUDED
#define POINT_H_INCLUDED
struct Point* createDefaultValues();
#endif // POINT_H_INCLUDED
point.c文件有如下代码:
#include <stdlib.h>
struct Point {
int x;
int y;
};
struct Point* createDefaultValues()
{
struct Point* point;
point = (struct Point*) malloc(sizeof(struct Point));
point->x = 2;
point->y = 1;
return point;
};
而main.c由以下几行组成:
#include <stdio.h>
#include <stdlib.h>
#include "point.h"
int main()
{
struct Point* cerc;
cerc = createDefaultValues();
printf("%d, %d", cerc->x, cerc->y);
return 0;
}
我在 printf() 行收到指向不完整类型错误的取消引用指针。我应该怎么做才能修复它?
我知道我可以在结构中分配默认值,但我不想那样做,我只想使用在我的 header 中找到的函数来分配它们。
最佳答案
您可以将结构定义移动到头文件中:
#ifndef POINT_H_INCLUDED
#define POINT_H_INCLUDED
struct Point {
int x;
int y;
};
struct Point* createDefaultValues();
#endif // POINT_H_INCLUDED
同时在point.c中包含头文件
关于c - 使用结构取消引用指向不完整类型的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36767822/