使用带有指针的 typedef const 结构
Using a typedef const struct with a pointer
好的,我在尝试使用特定结构时遇到了一个主要问题。
让我们从它开始:
typedef const struct x
{
....
} *y;
然后我有另一个包含 y 的结构,如下所示:
struct Z
{
...
y someName;
};
现在我的主要问题是我不知道如何使用它来为 someName 赋值。
如果我使用 x 并执行类似
的操作
struct x anotherName =
{
assignedThing
};
尝试做类似
的事情
Z.someName = anotherName;
未能说明 anotherName 不能转换为 y 类型。
显然我不能尝试通过创建 y 然后从 anotherName 分配特定部分来欺骗它,因为 y 是一个 const 结构并且以后不能更改。
我也做不到
struct y anotherName
失败了,我做不到
y anotherName =
{
};
因为它声称当原始结构有 14 个时,不止一个项目太多而无法初始化。
坦率地说,我在这里迷路了。无论如何,我是否可以实际创建一个已定义的 y 实例或使用我当前定义的 x 实例?我是否遗漏了一些明显的东西(我觉得我是)?我无法重写任何原始结构或将其从 const 更改或删除它是指针的事实,因此我们将不胜感激。
我认为这里的主要问题是您混淆了 struct x
和 y
的类型。
struct x
是 struct
而 y
是 pointer to struct
.
因此您不能分配 Z.someName = anotherName;
,因为 anotherName
是 struct x
类型,而 someName 是 y
类型。
你应该这样做
Z.someName = &anotherName
同样,你不能直接将结构赋值给指针类型的变量,如
y anotherName =
{
};
您创建了一个 struct x
变量并将其地址分配给 y
:
类型的变量
struct x someOtherName =
{
};
y anotherName = &someOtherName;
好的,我在尝试使用特定结构时遇到了一个主要问题。 让我们从它开始:
typedef const struct x
{
....
} *y;
然后我有另一个包含 y 的结构,如下所示:
struct Z
{
...
y someName;
};
现在我的主要问题是我不知道如何使用它来为 someName 赋值。
如果我使用 x 并执行类似
的操作struct x anotherName =
{
assignedThing
};
尝试做类似
的事情Z.someName = anotherName;
未能说明 anotherName 不能转换为 y 类型。
显然我不能尝试通过创建 y 然后从 anotherName 分配特定部分来欺骗它,因为 y 是一个 const 结构并且以后不能更改。
我也做不到
struct y anotherName
失败了,我做不到
y anotherName =
{
};
因为它声称当原始结构有 14 个时,不止一个项目太多而无法初始化。
坦率地说,我在这里迷路了。无论如何,我是否可以实际创建一个已定义的 y 实例或使用我当前定义的 x 实例?我是否遗漏了一些明显的东西(我觉得我是)?我无法重写任何原始结构或将其从 const 更改或删除它是指针的事实,因此我们将不胜感激。
我认为这里的主要问题是您混淆了 struct x
和 y
的类型。
struct x
是 struct
而 y
是 pointer to struct
.
因此您不能分配 Z.someName = anotherName;
,因为 anotherName
是 struct x
类型,而 someName 是 y
类型。
你应该这样做
Z.someName = &anotherName
同样,你不能直接将结构赋值给指针类型的变量,如
y anotherName =
{
};
您创建了一个 struct x
变量并将其地址分配给 y
:
struct x someOtherName =
{
};
y anotherName = &someOtherName;