我想访问我的结构点。结构内存是动态定位的。我遇到了我无法弄清楚的分段错误。从我的.h文件中我的结构定义如下:访问从另一个图中的数字的指针C
struct point{
double x;
double y;
};
struct figure{
char name[128];
int draw;
struct point *points;
};
extern struct figure *figures;
在我的.c文件我有:
struct figure *figures;
//initializing 10 figures
figures = malloc(10 * sizeof(struct figure));
//left the obvious NULL checking out for brevity
//I'm fairly sure this portion works for initializing 10 points for each figure
int i;
for(i = 0;i<10;i++){
figures[i].points = malloc(10 * sizeof(struct point));
//left out NULL checking again
}
除非这一点,这是我在哪里遇到问题之前检测到问题,实际上将值存储到点中。 注:指标可以是任何INT> = 0,只使用一个通用术语,为简单起见
figures[index].points[index]->x = 10;
figures[index].points[index]->y = 15;
与他的问题任何帮助将是非常美妙的。提前致谢。
用于这两个数字和点的相同索引? –
不,没有数量未知的数字,点数未知。我们从文件中获取数据,我忽略了大部分代码。刚刚用于更一般情况的索引 – Kris
figures [index] .points [index]是一个点,而不是一个指针指针。 –