复制到 C 中的嵌套结构

标签 c syntax copy structure

我正在尝试将结构 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/

相关文章:

c - 我们如何在具有两个不同指针的同一结构中进行类型定义? _t 这个词是做什么的?

c - 用C语言在文件末尾写入信息

syntax - 来自ocaml的Nest Let语法

c++ - Bison:由于冲突,规则在解析器中无用 [-Wother]

MySQL - 使用触发器复制表

使用 `pthread_create` 创建用户级线程或内核级线程?

c - 通用类型交换 C 函数

javascript - localStorage.getItem ('item' ) 比 localStorage.item 或 localStorage ['item' ] 更好吗?

处理复制文本的 JavaScript

c# - 无法使用 FileIOPermission 复制具有适当权限的文件