动态内存分配指向结构的指针数组 [英] dynamic memory allocation of an array of pointers to structures
问题描述
我一直在尝试为动态分配一系列指向结构的指针。因为它没有工作,我试图尽可能地减少问题。这就是我所拥有的:
Hi,
I''ve been trying to allocate dynamicaly an array of pointers to structures. As it didn''t work, I tried to reduce the problem as much as possible. This is what I have :
推荐答案
我一直在尝试为动态分配一系列指向结构的指针。因为它没有工作,我试图尽可能地减少问题。这就是我所拥有的:
Hi,
I''ve been trying to allocate dynamicaly an array of pointers to structures. As it didn''t work, I tried to reduce the problem as much as possible. This is what I have :
p [ i]不再是地址了...... p [i]是数组的元素而不是指向元素的指针所以你不能使用 - >。你如何使用calloc为2个大小为struct_p的对象的数组分配内存,所以应该没问题。我认为如果你用''替换'' - >''就行了。''
问候
p[i] is no more an adress...p[i] is an element of an array and not a pointer to an element so you can''t use ->. How you use calloc you allocate memory for an array of 2 objects of size of struct_p, so that should be fine. I think it should work if you replace ''->'' by ''.''
Regards
谢谢你的支持回答。
我使用sizeof(struct_p *)而不是sizeof(struct_p)。它有什么区别吗?我认为,这样,数组的元素就是指针。
问候
Thank you for your answer.
I used sizeof(struct_p *) and not sizeof(struct_p). Does''nt it make any difference? I thought that, this way, the elements of the array would be pointers.
Regards
感谢您的回答。
我使用sizeof(struct_p *)而不是sizeof(struct_p)。它有什么区别吗?我想,这样,数组的元素就是指针。
问候
Thank you for your answer.
I used sizeof(struct_p *) and not sizeof(struct_p). Does''nt it make any difference? I thought that, this way, the elements of the array would be pointers.
Regards
其实我不是''知道运算符sizeof,但从我的理解sizeof没有定义任何类型(即指针或变量)只是给出八位字节的大小。考虑到这一点你应该使用sizeof(struct_p),因为你想要为p分配内存,以便它可以容纳两个struct_p类型的对象。
使用sizeof(struct_p *)意味着p可以容纳2个对象指向struct_p的指针大小。
我不确定这个,但我认为就是这样。
你还得错吗使用''。''而不是'' - >''?
Actually I don''t know well the operator sizeof, but from what I understand sizeof doesn''t define any type (i.e. pointer or variable) just giving the size in octets. Considering this you should use sizeof(struct_p), because you want allocate memory to p so that it can hold two objects of type struct_p.
Using sizeof(struct_p *) it means that p can hold 2 objects of size of a pointer to struct_p.
I''m not sure about this, but I think it is this way.
Did you still get errors using the ''.'' instead of the ''->'' ?
这篇关于动态内存分配指向结构的指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!