Hi guys,

I am running gcc 6.4.0 in cygwin in Windows 10. I was looking for a 4D calloc array.

I tried this neat one by brewbuck from March 2007:
dynamically allocating 4D arrays

Code:
int ****array_4d;
int dim1 = 5;
int dim2 = 5;
int dim3 = 5;
int dim4 = 5;
int i, j, k;

array_4d = calloc(dim1, sizeof(array_4d[0]));
for(i = 0; i < dim1; i++)
{
    array_4d[i] = calloc(dim2, sizeof(array_4d[0][0]));
    for(j = 0; j < dim2; j++)
    {
        array_4d[i][j] = calloc(dim3, sizeof(array_4d[0][0][0]));
        for(k = 0; k < dim3; k++)
        {
            array_4d[i][j][k] = calloc(dim4, sizeof(array_4d[0][0][0][0]));
        }
    }
}
The code seems so straight forward and makes sense at a glance. However I got this error when I tried to compile it:

$ gcc -o 4dcalloc 4dcalloc.c
4dcalloc.c:18:1: warning: data definition has no type or storage class
array_4d = calloc(dim1, sizeof(array_4d[0]));
^~~~~~~~
4dcalloc.c:18:1: warning: type defaults to ‘int’ in declaration of ‘array_4d’ [-Wimplicit-int]
4dcalloc.c:18:1: error: conflicting types for ‘array_4d’
4dcalloc.c:11:9: note: previous declaration of ‘array_4d’ was here
int ****array_4d;
^~~~~~~~
4dcalloc.c:18:40: error: subscripted value is neither array nor pointer nor vector
array_4d = calloc(dim1, sizeof(array_4d[0]));
^
4dcalloc.c:19:1: error: expected identifier or ‘(’ before ‘for’
for(i = 0; i < dim1; i++)
^~~
4dcalloc.c:19:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
for(i = 0; i < dim1; i++)
^
4dcalloc.c:19:23: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘++’ token
for(i = 0; i < dim1; i++)
^~

I don't know how else to ask this question, but here it is: how do I make these warnings go away please?

The warning says array_4d has no type, but I thought it does. It has been declared int **** type hasn't it?

I then tried this piece of code by Stack Overflow from April 2004:

4-dimensional array contiguous allocation

Code:
#include <stdio.h>
#include <stdlib.h>

/*
Our example.

int arr[2][2][3][4] = {
    {
        { {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11} },
        { {12, 13, 14, 15}, {16, 17, 18, 19}, {20, 21, 22, 23} },
    },
    {
        { {24, 25, 26, 27}, {28, 29, 30, 31}, {32, 33, 34, 35} },
        { {36, 37, 38, 39}, {40, 41, 42, 43}, {44, 45, 46, 47} },
    }
};
*/

int main() {
    /* Local variables */
    int i=0, j=0, k=0, l=0, f, m=0;
    int ****arr = NULL;

    /* arr[2] */
    arr = malloc(2 * sizeof *arr);
    if (arr) {
        /* arr[2][2] */
        for (i = 0; i < 2; i++) {
            /* Allocate memory */
            arr[i] = malloc(2 * sizeof *arr);

            /* If allocation failed */
            if (arr[i] == NULL) {
                    /* Free any memory that was previously used */
                    for (f = i; f >= 0; f--) {
                        if (arr[f] != NULL) {
                            free(arr[f]);
                            arr[f] = NULL;
                        }
                    }
                    /* Free any memory that was previously used from beginning */
                    if (arr != NULL) {
                        free(arr);
                        arr = NULL;
                    }

                    /* Terminate program */
                    return 0;
            }

            /* arr[2][2][3] */
            for (j = 0; j < 2; j++) {
                /* Allocate memory */
                arr[i][j] = malloc(3 * sizeof *arr);

                /* If allocation failed */
                if (arr[i][j] == NULL) {
                    /* Free any memory that was previously used */
                    for (f = j; f >= 0; f--) {
                        if (arr[i][f] != NULL) {
                            free(arr[i][f]);
                            arr[i][f] = NULL;
                        }
                    }
                    /* Free any memory that was previously used in previous loop */
                    for (f = i; f >= 0; f--) {
                        if (arr[f] != NULL) {
                            free(arr[f]);
                            arr[f] = NULL;
                        }
                    }
                    /* Free any memory that was previously used from beginning */
                    if (arr != NULL) {
                        free(arr);
                        arr = NULL;
                    }

                    /* Terminate program */
                    return 0;
                }

                /* arr[2][2][3][4] */
                for (k = 0; k < 3; k++) {
                    /* Allocate memory */
                    arr[i][j][k] = malloc(4 * sizeof *arr);

                    /* If allocation failed */
                    if (arr[i][j][k] == NULL) {
                        /* Free any memory that was previously used */
                        for (f = k; f >= 0; f--) {
                            if (arr[i][j][f] != NULL) {
                                free(arr[i][j][f]);
                                arr[i][j][f] = NULL;
                            }
                        }
                        /* Free any memory that was previously used */
                        for (f = j; f >= 0; f--) {
                            if (arr[i][f] != NULL) {
                                free(arr[i][f]);
                                arr[i][f] = NULL;
                            }
                        }
                        /* Free any memory that was previously used in previous loop */
                        for (f = i; f >= 0; f--) {
                            if (arr[f] != NULL) {
                                free(arr[f]);
                                arr[f] = NULL;
                            }
                        }
                        /* Free any memory that was previously used from beginning */
                        if (arr != NULL) {
                            free(arr);
                            arr = NULL;
                        }

                        /* Terminate program */
                        return 0;
                    }
                }
            }
        }
    }else {
        return 0;
    }

    /* Make sure we can write without failure */
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            for (k = 0; k < 3; k++) {
                for (l = 0; l < 4; l++) {
                    arr[i][j][k][l] = m++;
                }
            }
        }
    }

    /* Print array style for example sake */
    printf("In example to: int arr[2][2][3][4];\n\n");
    printf("int ****arr = {\n");
    printf("\t{\n");
    printf("\t\t{ {%d, %d, %d, %d}, {%d, %d, %d, %d}, {%d, %d, %d, %d} },\n", arr[0][0][0][0], 
        arr[0][0][0][1], arr[0][0][0][2], arr[0][0][0][3], arr[0][0][1][0], arr[0][0][1][1], 
        arr[0][0][1][2], arr[0][0][1][3], arr[0][0][2][0], arr[0][0][2][1], arr[0][0][2][2], 
        arr[0][0][2][3]);
    printf("\t\t{ {%d, %d, %d, %d}, {%d, %d, %d, %d}, {%d, %d, %d, %d} },\n", arr[0][1][0][0], 
        arr[0][1][0][1], arr[0][1][0][2], arr[0][1][0][3], arr[0][1][1][0], arr[0][1][1][1], 
        arr[0][1][1][2], arr[0][1][1][3], arr[0][1][2][0], arr[0][1][2][1], arr[0][1][2][2], 
        arr[0][1][2][3]);
    printf("\t},\n");
    printf("\t\t{ {%d, %d, %d, %d}, {%d, %d, %d, %d}, {%d, %d, %d, %d} },\n", arr[1][0][0][0], 
        arr[1][0][0][1], arr[1][0][0][2], arr[1][0][0][3], arr[1][0][1][0], arr[1][0][1][1], 
        arr[1][0][1][2], arr[1][0][1][3], arr[1][0][2][0], arr[1][0][2][1], arr[1][0][2][2], 
        arr[1][0][2][3]);
    printf("\t\t{ {%d, %d, %d, %d}, {%d, %d, %d, %d}, {%d, %d, %d, %d} },\n", arr[1][1][0][0], 
        arr[1][1][0][1], arr[1][1][0][2], arr[1][1][0][3], arr[1][1][1][0], arr[1][1][1][1], 
        arr[1][1][1][2], arr[0][0][1][3], arr[1][1][2][0], arr[1][1][2][1], arr[1][1][2][2], 
        arr[1][1][2][3]);
    printf("\t}\n};");

    /* Free Memory */
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            for (k = 0; k < 3; k++) {
                free(arr[i][j][k]);
                arr[i][j][k] = NULL;
            }
            free(arr[i][j]);
            arr[i][j] = NULL;
        }
        free(arr[i]);
        arr[i] = NULL;
    }
    free(arr);
    arr = NULL;

    return 0;
}

It compiled and ran like a charm.

However I like the first piece of code better. It's a lot neater and easier for me to work with. I don't want to work with two pages of code for a 4d calloc array because my actual code will be much more complicated. This 4d core will be called upon again and again by my program.

How may I amend the first piece of code so it will run without error please?

Thank you.