我目前正在为下周一的考试做练习考试,我遇到了一些让我感到困惑的事情!
我有以下结构:
struct shape2d {
float x;
float y;
};
struct shape3d {
struct shape2d base;
float z;
};
struct shape {
int dimensions;
char *name;
union {
struct shape2d s1;
struct shape3d s2;
} description;
};
typedef struct shape Shape;
我必须使用以下签名创建一个“创建”形状的函数:
Shape *createShape3D(float x, float y, float z, char *name);
因为我正在处理结构的联合,所以我不太确定如何初始化我需要的所有字段!
这是我到目前为止所拥有的:
Shape *createShape3D(float x, float y, float z, char *name) {
Shape *s = (Shape *) malloc(sizeof(Shape));
s->dimensions = 3;
s->name = "Name...";
// How can I initialize s2?
return s;
}
任何帮助将不胜感激!