结构体指针数组 vs 结构体数组
来源:互联网 发布:网络大专报名 编辑:程序博客网 时间:2024/05/23 12:26
文章内容来自StackOverFlow,文章在这里。
下面两个数组的区别是什么?
1.struct mystruct *ptr = (struct test *)malloc(n*sizeof(struct test));
and
2.struct mystruct **ptr = (struct test *)malloc(n*sizeof(struct test *));
第一个创建包含n个test结构的数组,第二个创建包含n个指向struct test结构体的指针。
对于第二种情况,我们需要为每一个数组变量分配空间,如下:
struct mystruct **ptr = malloc(n*sizeof(struct test *));for (int i = 0; i != n ; i++) { ptr[i] = malloc(sizeof(struct test));}ptr[0]->field1 = value;...// Do not forget to free the memory when you are done:for (int i = 0; i != n ; i++) { free(ptr[i]);}free(ptr);
下面的使用方式是不正确的
struct mystruct **ptr = malloc(n*sizeof(struct test ));ptr[0]->filed1 = value;对于malloc而言,结构体和指针都是一样的,其都会将其转化为字节,所以上述这种用法不会自动将分配的内存转化为struct mystruct **。
- 结构体指针数组 vs 结构体数组
- 结构体指针数组 vs 结构体数组
- 结构体数组指针
- 结构体指针数组
- 结构体指针访问结构体数组
- 结构体数组和结构体指针
- 结构体指针和结构体数组
- 结构体&结构体指针&结构体指针数组
- 数组、结构体、指针1
- 数组、结构体、指针2
- 数组、结构体、指针3
- 数组、结构体、指针4
- 结构体指针和数组
- 数据结构 - 指针,数组,结构体
- 结构体数组指针memset
- 基本指针,数组,结构体
- 结构体数组与指针
- 结构体,数组,指针,地址
- 异常积累:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in
- MATLAB在Ubuntu下配置到applications menu
- 顺序表的应用举例
- Mahout实例(UserCF,ItermCF,SlopOne)
- dbgrideh使用
- 结构体指针数组 vs 结构体数组
- Hadoop的MapFile, SetFile, ArrayFile 及 BloomMapFile文件格式<转>
- 单链表的应用举例
- js使 input file获得焦点,并且清空该值。
- 百度地图SDK集成搜索服务
- 游戏开发入门
- 百度地图API新版申请API Key
- 详解大端模式和小端模式
- 1. 初识Deep Learning