c 如何在结构体中创建动态数组指针
我正在接受用户输入的数组内元素的数量。该数组位于我的结构“聚合物”内部
struct Polymer
{
int length;
struct Monomer *monomer;
}polymer;
,主要是,我正在创建一个新的单体数组指针,并将“聚合物”中的单体指针设置为它,
struct Monomer *monomers[size];
polymer.monomer = momomers;
我收到错误“分配来自不兼容的指针类型”,我认为这是因为我们是将单体数组指针转换为单体。如何将其声明为结构中的单体数组指针?
I am taking in input from the user for the number of elemenets inside an array. The array is inside my struct 'Polymer'
struct Polymer
{
int length;
struct Monomer *monomer;
}polymer;
In main, I am creating a new monomer array pointer and setting the mononomer pointer in "Polymer" to it
struct Monomer *monomers[size];
polymer.monomer = momomers;
I am getting the error "Assignment from incompatible pointer type" which I assume is because we are converting a monomer array pointer to a monomer. How do I declare it as a monomer array pointer in the struct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您可能需要一个单体数组时,您正在声明一个单体指针数组。删除
*
:You are declaring an array of monomer pointers when you probably want an array of monomers. Drop the
*
:monomers
是一个指针数组。它们没有指向任何有效位置并且具有垃圾值。而Polymer::monomer
是一个指针。指针数组与指针的类型不兼容。相反,尝试 -
现在这个语句 2 是有效的,因为数组衰减为指针。
monomers
is an array of pointers. They aren't pointing to any valid locations and has garbage values. WhilePolymer::monomer
is a pointer. Array of pointers isn't type compatible to just a pointer.Instead try -
Now this statement 2 is valid because array decays to a pointer.