0

I have to create a dynamic and 3 dimensional integer-Array in C.

But I have to create the pointers separately and use malloc. I know how to create 2d Array with malloc but I get confused using the following method, and i couldn't really find other question similar to this.

2x2x2 Integer-Array

First step:

int *firstD;  
firstD = (int) malloc(2*sizeof(int));

Second step:

int *secondD;  
secondD = (int) malloc(2 * firstD * sizeof(int));

Third step:

int *thirdD;  
thirdD = (int) malloc(2 * secondD * sizeof(int));

I think maybe I have to add pointers in the starting (int*) and increase it every stepp by one more pointer?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
  • Casting results of `malloc` to `int` is a bad idea because it may break the pointer. Casting results of `malloc` to pointers is also [considered as a bad idea](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – MikeCAT Dec 06 '20 at 05:22
  • If the 2nd and 3rd dimensions are fixed you can do a single allocation and free using a pointer to 2D array as your type. Also, while multi-multi-dimensional objects are perfectly fine in C, sometimes they are really needed -- but, often times it can also mean you need to rethink your data handling needs. – David C. Rankin Dec 06 '20 at 05:23
  • Thank you for the reply but I explicit have to use three steps (create one pointer in every step). This is for a exercise sheet my professor created for learning for the test and I am forced to use this method because of the way the points are given. I can not use a single line or other functions. Also I have to work with variable_name = (datatype) malloc(....) format. – Vanos Sege Dec 06 '20 at 05:32

1 Answers1

1
  1. Allocate an array to store pointers to arrays of row pointers.
  2. Allocate arrays to store row pointers.
  3. Allocate arrays to store each rows.
#include <stdlib.h>

int main(void) {
    int size1 = 2, size2 = 2, size3 = 2;
    int*** array;
    array = malloc(sizeof(int**) * size1); // 1
    for (int i = 0; i < size1; i++) {
        array[i] = malloc(sizeof(int*) * size2); // 2
        for (int j = 0; j < size2; j++) {
            array[i][j] = malloc(sizeof(int) * size3); // 3
        }
    }

    // free arrays
    for (int i = 0; i < size1; i++) {
        for (int j = 0; j < size2; j++) {
            free(array[i][j]);
        }
        free(array[i]);
    }
    free(array);

    return 0;
}

I wrote type names explicitly in the above example, but I suggest using dereferencing to obtain size of elements to allocate is better to prevent causing typos.

#include <stdlib.h>

int main(void) {
    int size1 = 2, size2 = 2, size3 = 2;
    int*** array;

    // allocate arrays
    array = malloc(sizeof(*array) * size1); // 1
    for (int i = 0; i < size1; i++) {
        array[i] = malloc(sizeof(*array[i]) * size2); // 2
        for (int j = 0; j < size2; j++) {
            array[i][j] = malloc(sizeof(*array[i][j]) * size3); // 3
        }
    }

    // free arrays
    for (int i = 0; i < size1; i++) {
        for (int j = 0; j < size2; j++) {
            free(array[i][j]);
        }
        free(array[i]);
    }
    free(array);

    return 0;
}

Onitted in the above examples to make them simple, but you should check results of malloc() to see if the allocations are successful.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70