[英]Using a typedef const struct with a pointer
Okay so I have been having a major issue trying to use a specific struct. 好的,我一直在尝试使用特定结构时遇到重大问题。 Lets start with it:
让我们开始吧:
typedef const struct x
{
....
} *y;
and then I have another struct that has y in it like so: 然后我有另一个具有y的结构,如下所示:
struct Z
{
...
y someName;
};
Now my main issue here is that I cannot figure out how to use this to assign something to someName. 现在,我的主要问题是我无法弄清楚如何使用它来为someName分配某些内容。
If I use x and do something like 如果我用x做类似的事情
struct x anotherName =
{
assignedThing
};
trying to do something like 试图做类似的事情
Z.someName = anotherName;
fails saying that anotherName cant be cast as y type. 无法说不能将anotherName强制转换为y类型。
Obviously I cant try to cheat it by making ay and then assigning the specific part from anotherName because y is a const struct and cant be changed later. 显然,我无法尝试通过ay作弊,然后从anotherName分配特定部分,因为y是一个const结构,以后无法更改。
I also cant do 我也做不到
struct y anotherName
that fails, and I cant do 失败了,我做不到
y anotherName =
{
};
because it claims that anymore than one item is too many to initialize when the original struct has like 14. 因为它声称当原始结构像14时,不止一个项目太多而无法初始化。
So frankly im lost here. 坦率地说,我在这里迷路了。 Is there anyway for me to actually create a defined instance of y or use my currently defined instance of x?
无论如何,我有没有实际创建y的定义实例或使用x的当前定义实例? Am I missing something obvious (I feel like I am)?
我是否缺少明显的东西(我感觉自己)? Im not in a position where I can rewrite any of the original structs or change it from const or remove the fact that its a pointer so any insight here would be appreciated.
我不在可以重写任何原始结构或从const更改它的位置,也不能删除它的指针这一事实,因此在这里的任何见识都将受到赞赏。
I think the main issue here is that you're confusing the types of struct x
and y
. 我认为这里的主要问题是您混淆
struct x
和y
的类型。
struct x
is a struct
whereas y
is a pointer to struct
. struct x
是一个struct
而y
是一个pointer to struct
的pointer to struct
。
So you can't assign Z.someName = anotherName;
所以你不能分配
Z.someName = anotherName;
as anotherName
is of type struct x
and someName is of type y
. 因为
anotherName
的类型为struct x
,someName的类型为y
。
You should instead do 你应该做
Z.someName = &anotherName
Similarly, you can't assign the struct to the pointer typed variable directly as 同样,您不能将结构直接分配给指针类型变量
y anotherName =
{
};
You create a struct x
variable and assign it's address to variable of type y
: 您创建一个
struct x
变量,并将其地址分配给y
类型的变量:
struct x someOtherName =
{
};
y anotherName = &someOtherName;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.