• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C语言动态创建4维数组

OC/C/C++ 水墨上仙 2587次浏览

C语言动态创建4维数组

#include <stdio.h>
#include <stdlib.h>
 
/********************************************************/
/*                                                      */
/*                                                      */
/*    Initialise a 4-d array with successive numbers.   */
/*                                                      */
/*                                                      */
/*     The array is formed dynamically using malloc.    */
/*                                                      */
/*     g.watt.porteous (gemera)     17th June 2012      */
/*                                                      */
/*                 graemewp@yahoo.com                   */
/********************************************************/
 
#define d4 4
#define d3 4
#define d2 4
#define d1 4
 
 
void initarr(int ****array,int hyp_sz,int plane_sz,int row_sz,int col_sz);
 
int main(void)
{
    int ****array;
    int hyp_sz = d4,plane_sz = d3,row_sz = d2,col_sz = d1;
    int h,i,j;
 
    array = malloc(hyp_sz * sizeof(int***));
    if (array == NULL)
    {
        fprintf(stderr,"Out Of Memory");
        exit(EXIT_FAILURE);
    }
 
    for(h = 0;h < hyp_sz;h++)
    {
        array[h] = malloc(plane_sz * sizeof(int**));
        if(array[h] == NULL)
        {
            fprintf(stderr,"Out Of Memory");
            exit(EXIT_FAILURE);
        }
 
        for(i = 0;i < plane_sz;i++)
        {
            array[h][i] = malloc(row_sz * sizeof(int*));
            if(array[h][i] == NULL)
            {
                fprintf(stderr,"Out Of Memory");
                exit(EXIT_FAILURE);
            }
            for(j = 0;j < row_sz;j++)
            {
                array[h][i][j] = malloc(col_sz * sizeof(int));
                if(array[h][i][j] == NULL)
                {
                    fprintf(stderr,"Out Of Memory");
                    exit(EXIT_FAILURE);
                }
            }
        }
    }
    printf("\n\n");
 
    initarr(array,hyp_sz,plane_sz,row_sz,col_sz);   // initialise and display array
 
    getchar();
    return 0;
}
 
void initarr(int ****array,int hyp_sz,int plane_sz,int row_sz,int col_sz)
{
    int g,h,i,j;
 
    for(g = 0 ;g < hyp_sz; g++)
    {
        for(h = 0;h < plane_sz; h++)
        {
            for(i = 0;i < row_sz; i++)
            {
                for(j = 0;j < col_sz; j++)
                {
                    array[g][h][i][j] = (g*plane_sz*row_sz*col_sz) + (h*row_sz*col_sz) + (i*col_sz )+ j + 1;
                    printf("%5d", array[g][h][i][j]);
                }
                printf("\n");
            }
            printf("\n");
        }
        printf("\n\n");
    }
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C语言动态创建4维数组
喜欢 (0)
加载中……