gpt4 book ai didi

复制到 C 中的嵌套结构

转载 作者:行者123 更新时间:2023-11-30 20:57:07 24 4
gpt4 key购买 nike

我正在尝试将结构 x 复制到另一个嵌套有 x 的结构 y 中。

例如:

#define DATASIZE 128

typedef struct {
char data[DATASIZE];
} x_TYPE;


typedef struct {
int number;
x_TYPE nested_x;
enum boolean error;
} y_TYPE;

/* ---- Values for the type field in xy_union ---- */
#define TYPE_IS_X 0;
#define TYPE_IS_Y 1;

typedef struct {
union { /* structure containing x_object */
x_TYPE x_object; /* or y_object as a union */
y_TYPE y_object;
} u;
int type; /*One of: TYPE_IS_X, TYPE_IS_Y */

} XY_TYPE;

这就是我目前复制的方式:

copyXY(XY_TYPE *xx)
{
XY_TYPE *yy; /* assume this is allocated already */


yy->u.y_object.nested_x = *xx; /* ERROR LINE */


return 0;
}

我收到编译器错误:错误:从类型“XY_TYPE”分配给类型“x_TYPE”时,类型不兼容。

如果有人知道为什么会发生这种情况,请告诉我。

最佳答案

这是您要找的吗?

XY_TYPE *yy; /* assume this is allocated already */

void copyXIntoYY(XY_TYPE *xx)
{
yy->u.y_object.nested_x = xx->u.x_object;
}

从问题中尚不清楚您想要做什么。

关于复制到 C 中的嵌套结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14446938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com