我想動態分配結構數組並對它們執行操作,但我一直運行到分段錯誤。有人能幫助我嗎?什麼導致c代碼段錯誤,功能動態分配
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void *malloc(size_t size);
typedef struct {
double x;
double y;
} coords;
struct figure {
char fig_name[128];
int coordcount, size_tracker;
coords *pointer;
} fig;
void init_fig(int n, struct figure **point)
{
printf("%u\n", sizeof(coords));
point[n]->pointer = malloc(sizeof(coords) * 20); <-------SEGFAULT
if (point[n]->pointer == NULL){
exit(-1);
}
point[n]->pointer[19].x = 2;
point[n]->pointer[0].x = 1;
point[n]->pointer[0].y = 2;
point[n]->pointer[7].x = 100;
}
int main()
{
int numfigs = 1;
struct figure * point;
point = malloc(sizeof(struct figure) * 16);
point = &fig;
point[1].coordcount = 1;
init_fig(numfigs, &point);
return 0;
}
我標記了第一個seg故障發生的地方(使用ddd)。我沒有得到的是,我可以在主要操作點[1],但不能在任何其他功能。
這不是我的項目它只是一個簡短的測試文件,我一直使用,試圖找出爲什麼IM葛亭賽格故障 – wenincode