#include <stdio.h>
#include <stdlib.h>
int main()
{
float ***p = NULL; // p[pen_num][row][col];
int i, j, k;
int pen_num = 3, row = 4, col = 3; // 3个栏位,每个栏位4行3列
/* 申请3个栏指针 */
p = (float ***)malloc(pen_num * sizeof(float **));
if (NULL == p)
{
printf("malloc fail\n");
return -1;
}
for (i = 0; i < pen_num; i++)
{
/* 申请4个行指针 */
p[i] = (float **)malloc(row * sizeof(float *));
if (NULL == p[i])
{
printf("malloc fail\n");
return -1;
}
for (j = 0; j < row; j++)
{
/* 为每个行指针申请列空间 */
p[i][j] = (float *)malloc(col * sizeof(float));
if (NULL == p[i][j])
{
printf("malloc fail\n");
return -1;
}
}
}
/* 赋值 */
for (i = 0; i < pen_num; i++)
{
for (j = 0; j < row; j++)
{
for (k = 0; k < col; k++)
{
p[i][j][k] = i + j + k +
c malloc实现:动态申请三维数组
最新推荐文章于 2024-05-06 15:42:03 发布