C/C++ : 结构上的指针
C/C++ : Pointer on Pointer on structure
我有 2 个结构:
struct A {
int m1;
int m2;
}
第二个结构体作为前一个结构的成员:
struct Temp_A {
A a;
}
然后我在我的程序中有:
Temp_A** temp_a;
所以我的问题是:
如何为
temp_a
分配内存?如何访问
a
?它应该是某种(*temp_a)->a...
谢谢!
如何为temp_a分配内存?
//1 here is number of pointer elements you want as temp_a is pointer to pointer
// or for simplicity array of pointers.
temp_a = malloc(sizeof(*temp_a)* 1);
//then you should allocate temp_a[0] too
temp_a[0] = malloc(sizeof(**temp_a));
如何访问
(*temp_a)->a
//and tehn
(*temp_a)->a.m1
//you can access as too
temp_a[0]->a
首先,您必须声明Temp_A
如下:
struct Temp_A {
struct A a;
}
现在,Temp_A**
指向指向Temp_A
的指针,因此您可以分配类似
temp_a = malloc(sizeof(Temp_A*));
(*temp_a) = malloc(sizeof(Temp_A));
您可以像访问一样
(*temp_a)->a
相关文章:
- 如何访问指针结构的成员变量?
- sizeof 函数如何在带和不带位字段的结构上工作?(填充)
- 未使用的 asm() 在不受支持的体系结构上的行为
- 复制引用结构上的赋值
- 有了memory_order_relaxed,原子变量的总修改顺序如何在典型体系结构上得到保证
- 如何将变量从变量传递到指针结构对象
- 如何从模板类重载创建的指针对象上的运算符?
- C 嵌套结构上的 g++ 编译错误
- Qt单元测试:指针列表上的QCOMPARE函数
- 结构上溢出的整数加法
- 在通过 P/Invoke 获取的 C++ 结构上设置 C# 回调
- 在包含浮点数的结构上使用 memset()
- C++ abort() 在函数内的抽象类对象指针调用上
- 如何在函数中检索指针结构
- C++ 如何在智能指针<string>上使用字符串::函数
- 指针类型上的 STL 函数对象
- C/C++ : 结构上的指针
- C++在结构上使用指针并将其保存在文件中
- 在哪些体系结构上计算无效指针不安全
- 为什么在非指针结构上进行类型转换会出现语法错误