Hi Experts
for a homework im trying to fill and print a quadruple cube using a 4 times pointer
Code:
#include <stdio.h>
#include <stdlib.h>
#define DIM 4
void printCube(char**** cube, int dim) {
int x, y, z;
for(z = 0; z < dim; z++) {
for(y = 0; y < dim; y++) {
for(x = 0; x < dim; x++) {
printf("%c ", *cube[z][y][x]);
}
printf("\n");
}
printf("------------------------------------\n");
}
}
int main() {
char*** cube = (char***)malloc(sizeof(char**) * DIM);
int x, y, z;
for(z = 0; z < DIM; z++) {
cube[z] = (char**)malloc(sizeof(char**) * DIM);
for(y = 0; y < DIM; y++) {
cube[z][y] = (char*)malloc(sizeof(char*) * DIM);
for(x = 0; x < DIM; x++) {
cube[z][y][x] = ((x + y + z) % 26) + 'A';
}
}
}
printCube(&cube, DIM);
for(z = 0; z < DIM; z++) {
for(y = 0; y < DIM; y++) {
for(x = 0; x < DIM; x++) {
free(cube[z][y][x]);
}
free(cube[z][y]);
}
free(cube[z]);
}
free(cube);
return 0;
}
I tried now for several hours to correct this code. I found out that the error should be at this statement printf("%c ", *cube[z][y][x]); because printing without the pointer works fine. But im unsure how i can should use the pointer to print the cube...
Really would appreciate your support