- UID
- 487269
- 在线时间
- 0 小时
- 最后登录
- 1970-1-1
- 注册时间
- 2013-7-8
- 宅魂
- 114 点
- 贡献
- 24 点
- 宅币
- 1449 枚
- 灵石
- 0 块
- 元气(技能点)
- 0 点
- 活跃
- 0 ℃
- 听众
- 7
- 收听
- 0
该用户从未签到
Continue
- 积分
- 1863
|
发表于 2013-7-8 15:46:46
|
显示全部楼层
当然可以
[mw_shl_code=c,true]int** alloc_memory_for_2D_array(int m, int n){
int **ptr;
int i;
ptr = (int **)malloc(m*sizeof(int *));
for (i=0; i<m; i++){
ptr = (int *)malloc(n*sizeof(int));
}
return ptr;
}[/mw_shl_code]
例子:
[mw_shl_code=c,true]int **array_2d;
const int width = 10;
const int height = 20;
array_2d = alloc_memory_for_2D_array(height, width);
if (array_2d == NULL){
printf("Fail to allocate memory.");
}[/mw_shl_code]
一定不要忘记检查分配是否成功
最后还要有个函数负责free
[mw_shl_code=c,true]void free_memory_for_2D_array(int **ptr, int m){
int i;
for (i=0; i<m; i++){
if (ptr){
free(ptr);
ptr = NULL;
}
}
if (ptr){
free(ptr);
ptr = NULL;
}
}[/mw_shl_code] |
|