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