0

I am trying to solve out how create a function that copies the contents from one 3D array to another 3D array which must be user input. I've managed to make the program under one main function however the desired way of implementing this is through having two or more functions.

The first attempt was as follows (i.e. incorrect - in one main function)

main()
{
    int x,y,z;
    printf("Enter x value.\n");
    scanf("%d", &x);
    printf("Enter y value.\n");
    scanf("%d", &y);
    printf("Enter z value.\n");
    scanf("%d", &z);



    printf("The size of the array is %d.\n", x*y*z);
    /* 3D array declaration*/
    int disp[x][y][z];
    int cpydisp[x][y][z];


    /*Counter variables for the loop*/
    int i, j, k;
    for(i=0; i<x; i++) {
        for(j=0;j<y;j++) {
            for (k = 0; k < z; k++) {
                printf("Enter value for disp[%d][%d][%d]:", i, j, k);
                scanf("%d", &disp[i][j][k]);
            }
        }
    }

    memcpy(cpydisp,disp, sizeof(disp));

    //Displaying array elements
    printf("Three Dimensional array elements:\n");
    for(i=0; i<x; i++) {
        for(j=0;j<y;j++) {
            for (k = 0; k < z; k++) {
                printf("%d ", cpydisp[i][j][k]);
            }
            printf("\n");
        }
    }

return 0; }

After doing research and what not I stumbled upon this were similar to me it requires a user input however this is in 1D and doesn't copy the contents. User input array size C

After looking at that I'm currently trying to configure the following, however when trying to print out the original array (not even the copied one) the system is crashing.

#include <stdlib.h>
#include <stdio.h>
#include <memory.h>

void printArray(int ***cpydisp,int x,int y,int z) {
    int i,j,k;
    printf("Three Dimensional array elements:\n");
    for(i=0; i<x; i++) {
        for(j=0;j<y;j++) {
            for (k = 0; k < z; k++) {
                printf("%d ", cpydisp[i][j][k]);
            }
            printf("\n");
        }
    }
}



void copy3d(size_t x, size_t y, size_t z, int d[x][y][z], int src[x][y][z]) {
    printf("s[%zu][%zu][%zu]\nSizes: d:%zu, d[]:%zu, d[][]:%zu, d[][][]:%zu\n\n",
           x, y, z, sizeof d, sizeof d[0], sizeof d[0][0], sizeof d[0][0][0]);
    // 'sizeof' on array function parameter 'src' returns
    //     size of 'int (*)[(sizetype)(y)][(sizetype)(z)]
    memcpy(d, src, sizeof d[0] * x);
}


int main(void) {
    int x,y,z;
    printf("Enter the size of the array:\n");
    scanf("%d", &x);
    printf("Enter the size of the array:\n");
    scanf("%d", &y);
    printf("Enter the size of the array:\n");
    scanf("%d", &z);

    // ask for enough memory to fit `count` elements,
    // each having the size of an `int`
    int ***array = malloc(x * y * z * sizeof(***array));

    int i, j, k;
    for(i=0; i<x; i++) {
        for(j=0;j<y;j++) {
            for (k = 0; k < z; k++) {
                printf("Enter value for display[%d][%d][%d]:", i, j, k);
                scanf("%d", &array[i][j][k]);
            }
        }
    }

    printArray(array, x, y, z);

    free(array);
}

Thanks In Advance

dude_98
  • 11
  • 1
  • 1
  • 5
  • 1
    You should read [Why can't we use double pointer to represent two dimensional arrays?](https://stackoverflow.com/questions/4470950/why-cant-we-use-double-pointer-to-represent-two-dimensional-arrays) – Ilja Everilä Jan 13 '19 at 17:49
  • 1
    See also [Function that copies a 3d array in C?](https://stackoverflow.com/questions/54127071/function-that-copies-a-3d-array-in-c/54135323#54135323)? – chux - Reinstate Monica Jan 13 '19 at 18:41
  • `int ***array` is not a 3D array or even any dimensional array. `array` is a pointer. `printArray(int ***cpydisp,int x,int y,int z)` is not for printing a 3D array either. – chux - Reinstate Monica Jan 13 '19 at 18:42
  • Try `int ***array = malloc(x * y * z * sizeof(***array));` --> `int array[x][y][z];` to make `array` a 3D array. – chux - Reinstate Monica Jan 13 '19 at 18:44
  • dude_98, If the problem is creating a UI to a 3D array here, consider editing the title from "Creating a function that copies contents of a 3d array" as copying is not the issue given "trying to print out the original array (not even the copied one) the system is crashing." – chux - Reinstate Monica Jan 13 '19 at 18:50
  • dude_98 Try a 1D case first. A key issue: Are you trying to create an _array_ like `int a1[x]`, or allocating memory for a 1D array with `int (*a2)[x] = malloc(sizeof *a2);` or allotting memory for a pointer to `int` like `int *a3 = malloc(sizeof *a3 * x`? – chux - Reinstate Monica Jan 13 '19 at 18:56

1 Answers1

0

This is not recommended... but what your missing is allocation memory to the array. For me to wrap my head around memory and multidimensional arrays, I like to think of them in steps and them set them up in steps also. So first, you malloc columns, then rows etc.

so you land up with this:

// get the columns mem sorted
int ***array = (int ***)malloc(y * sizeof(***array));

// loop through all the columns and give them enough mem for a row
for (int j = 0; j < y; j++) {
    array[j] = (int **)malloc(x * sizeof(**array));

    //loop through each row and give them them enough mem for z axis
    for (int i = 0; i < x; i++) {
        array[j][i] = (int *)malloc(x * sizeof(*array));
    }
}

int i, j, k;
for (i = 0; i < x; i++) {
    for (j = 0; j < y; j++) {
        for (k = 0; k < z; k++) {
            printf("Enter value for display[%d][%d][%d]:", i, j, k);
            scanf("%d", &array[i][j][k]);
        }
    }
}

This sets it all up and you can access the members. But doing this means at the end of the program you need to do the loop again and instead of malloc you need to free all those elements. Again this is not recommended. Read this or to save yourself some headache simulate a 3d array

testfile
  • 2,145
  • 1
  • 12
  • 31