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?

3
  • 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. Commented Dec 6, 2020 at 5: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. Commented Dec 6, 2020 at 5: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. Commented Dec 6, 2020 at 5:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.