【发布时间】:2011-11-01 11:17:08
【问题描述】:
我有两个结构:
struct b{int b;float d;}; and
struct a{int count; struct b* ptr;}
struct a *a_temp;
现在我为 10 个类型 b 的结构分配内存,并将地址放在结构 a 的 ptr 中。 (代码给了我,他们出于某种原因不想使用双指针)
a_temp = (struct a*)malloc(sizeof(struct a));
a_temp->ptr = (struct b*)malloc(10*sizeof(struct b));
struct b* b_temp;
我必须将 b 类型的第二个结构的地址加载到 temp_b。
我试过b_temp = a_temp->ptr[1];,它给出了错误
但是当我尝试使用它并使用它访问结构 b 的内容时,b_temp = &(a_temp->ptr[1]); 正在工作,这是为什么呢?
提前致谢
【问题讨论】:
标签: c pointers dynamic-allocation