Assign Memory to 3D array using triple pointer

后端 未结 5 870
别跟我提以往
别跟我提以往 2021-01-01 05:54

I have to assign memory to a 3D array using a triple pointer.

#include 
int main()
{
    int m=10,n=20,p=30;
    char ***z;
    z = (char***)          


        
5条回答
  •  不知归路
    2021-01-01 06:34

    To completely allocate a 3D dynamic array you need to do something like the following:

    #include 
    #include 
    
    int main()
    {
        int m=10,n=20,p=30;
        char ***z;
    
        z = malloc(m * sizeof(char **));
        assert(z != NULL);
        for (i = 0; i < m; ++i)
        {
            z[i] = malloc(n * sizeof(char *));
            assert(z[i] != NULL);
            for (j = 0; j < n; ++j)
            {
                z[i][j] = malloc(p);
                assert(z[i][j] != NULL);
            }
        }
        return 0;
    }
    

    Freeing the data is left as an exercise for the reader.

提交回复
热议问题