Thread: 4 times pointer

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    1

    4 times pointer

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Like this.

    printf("%c ", (*cube)[z][y][x]);

    Oh, and remove all your malloc casts.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,645
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    void printCube(char*** cube, int dim) {
        for (int z = 0; z < dim; z++) {
            for (int y = 0; y < dim; y++) {
                for (int x = 0; x < dim; x++)
                    printf("%c ", cube[z][y][x]);
                printf("\n");
            }
            printf("-------\n");
        }
    }
     
    int main() {
        int dim = 4;
        char*** cube = malloc(dim * sizeof *cube);
        *cube = malloc(dim * dim * sizeof **cube);
        for (int z = 1; z < dim; ++z)
            cube[z] = cube[z - 1] + 1;
        **cube = malloc(dim * dim * dim * sizeof ***cube);
        for (int z = 0; z < dim; ++z)
            for (int y = 1; y < dim; ++y)
                cube[z][y] = cube[z][y - 1] + 1;
     
        for (int z = 0; z < dim; z++)
            for (int y = 0; y < dim; y++)
                for (int x = 0; x < dim; x++)
                    cube[z][y][x] = ((x + y + z) % 26) + 'A';
     
        printCube(cube, dim);
     
        free(**cube);
        free(*cube);
        free(cube);
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 08-09-2011, 12:55 PM
  2. Outputing 2 times?
    By Tropicalia in forum C++ Programming
    Replies: 9
    Last Post: 09-23-2006, 09:21 PM
  3. Times.h
    By LearningC in forum C Programming
    Replies: 6
    Last Post: 10-02-2005, 09:09 AM
  4. Running times
    By chema124 in forum C Programming
    Replies: 2
    Last Post: 05-06-2003, 08:58 AM
  5. level up 6 times with 40 EXP!?!?!?!
    By Blizzarddog in forum Game Programming
    Replies: 15
    Last Post: 03-05-2003, 12:56 PM

Tags for this Thread