假设我有以下结构:
typedef struct plane_t Plane;
struct plane_t{
Point p1;
Point p2;
Point p3;
};
typedef struct arrangement_t* Arrangement;
struct arrangement_t{
//TODO add fields here
int maxPlanes;
int curPlanes;
Plane *planes;
};
我有以下功能:
Plane planeCreate(Point point1, Point point2, Point point3){
Plane newPlane = {{point1.x, point1.y, point1.z}, {point2.x, point2.y, point2.z}, {point3.x, point3.y, point3.z}};
return newPlane;
}
假设我正在编写一个函数,它将一个平面添加到 arrangment_t 结构内的数组平面中。
我可以执行以下操作:
arrangement->planes[arrangement->curPlanes] = planeCreate(plane.x, plane.y plane.z);
或者这个结构将在退出这个函数后“消失”,这意味着我必须通过以下方式:
arrangement->planes[arrangement->curPlanes] = malloc(sizeof(struct plane_t));
arrangement->planes[arrangement->curPlanes].x=plane.x;
arrangement->planes[arrangement->curPlanes].x=plane.y;
arrangement->planes[arrangement->curPlanes].x=plane.z;