malloc 申请二维数组
来源:互联网 发布:绘画书籍推荐 知乎 编辑:程序博客网 时间:2024/05/16 07:53
函数打包版#include <malloc.h>#include <stdio.h>#define ROW 4#define LINE 5int ** getArray(int row, int line){ int **p = NULL; int i =0; p = (int**)malloc(sizeof(int*)*row); for(; i<row; i++) p[i] = (int*)malloc(sizeof(int)*line); return p;}void freeArray(int **p, int row){ int i,j; for(i=0; i<row; i++) free(p[i]); free(p);}int main(){ int **p2Array = getArray(ROW,LINE); int i,j; for(i=0; i<ROW; i++) { for(j=0; j<LINE; j++) { p2Array[i][j] = i+j; } } for(i=0; i<ROW; i++) { for(j=0; j<LINE; j++) { printf("%d ",p2Array[i][j]); } putchar(10); } freeArray(p2Array,ROW); return 0;}-------------------------------------------------------------------------------------------------------------------------------#include <malloc.h>#include <stdio.h>#define ROW 4#define LINE 5int * getArray(int row, int line){ int *p = malloc(sizeof(int)*row*line); return p;}void freeArray(int *p){ free(p);}int main(){ int (*p2Array)[LINE] = (int(*)[LINE]) getArray(ROW,LINE); int i,j; for(i=0; i<ROW; i++) { for(j=0; j<LINE; j++) { p2Array[i][j] = i+j; } } for(i=0; i<ROW; i++) { for(j=0; j<LINE; j++) { printf("%d ",p2Array[i][j]); } putchar(10); } freeArray(p2Array); return 0;}
其本格式
#include <malloc.h>#include <stdio.h>int main(){ int (*a)[2] = malloc(sizeof(int)*3*2); a[0][0] =1; a[0][1] =2; a[1][0] =3; a[1][1] = 4; a[2][0] =5; a[2][1] = 6; printf("%d\t%d\t%d\t%d\t%d\t%d\n",a[0][0],a[0][1],a[1][0],a[1][1],a[2][0],a[2][1]); free(a); system("pause");} 或者:#include <malloc.h>#include <stdio.h>int main(){ int **a = malloc(sizeof(int)*3); a[0]= malloc(sizeof(int)*2); a[1]= malloc(sizeof(int)*2); a[2]= malloc(sizeof(int)*2); a[0][0] =1; a[0][1] =2; a[1][0] =3; a[1][1] = 4; a[2][0] =5; a[2][1] = 6; printf("%d\t%d\t%d\t%d\t%d\t%d\n",a[0][0],a[0][1],a[1][0],a[1][1],a[2][0],a[2][1]); free(a[0]); free(a[1]); free(a[2]); free(a); system("pause");}
1 0
- malloc 申请二维数组
- malloc动态申请二维数组
- 二维数组动态申请 malloc
- malloc申请二维数组的方法
- 如何用malloc申请二维数组
- 用malloc函数申请动态二维数组
- 用malloc申请一个二维数组
- 一个malloc,一个free,动态申请二维数组
- c中使用malloc动态申请二维数组
- C语言用malloc函数申请二维动态数组
- C语言用malloc函数申请二维动态数组
- malloc二维数组
- malloc分配二维数组
- malloc二维数组
- malloc free 二维数组
- 二维数组 malloc
- malloc 实现二维数组
- 动态申请二维数组。
- OpenGL: 通过射线求交实现物体的拾取
- 【Windows7】中, 如何对未分配的磁盘空间进行分区
- ArcGIS教程:MapGIS转换shp攻略
- 断电后重启Hbase出现KeeperErrorCode = DataInconsistency
- 发挥地方会更方便的分公司的方式的范德萨范德萨
- malloc 申请二维数组
- 很反感脚后跟分解分解法国和法国和地方的三个地方
- 未将服务器'66.77.88.72' 配置于 rpc
- 代码修改后运行结果同修改之前结果一样
- OpenGL: 3D中投影矩阵公式的推导
- Bouguet极线校正的方法
- 使用测试账号实测微信公众平台各接口
- 文本框只允许数字、字母,大写字母小写字母
- 在Viewpager的子Activity中startActivityForResult调用其他Activity时候遭遇到onActivityResult方法不响应的问题.