title: Modifying function that malloc's a 2d array to a 3d array in C
body: <p>I'm very new to C, this is the first program I'm writing in it. My professor gave us a function for allocating memory for a 2d array, called malloc2d. I am supposed to modify it to allocate memory for a 3d array, but being so new to C I am not sure how to go about it. I've tried looking at other malloc functions for 3d arrays but none of them look similar to the one I was given. Similarly, we have a free2d function that also needs to be modified for a 3d array. Here are the functions to be modified:</p>
  
  <pre><code>void** malloc2D(size_t rows, size_t cols, size_t sizeOfType){
      void* block = malloc(sizeOfType * rows * cols);
      void** matrix = malloc(sizeof(void*) * rows);
      for (int row = 0; row &lt; rows; ++row) {
          matrix[row] = block + cols * row * sizeOfType;
      }//for
      return matrix;
  }//malloc2D
  
  void free2D(void*** matrix){
      free((*matrix)[0]);
      free((*matrix));
      matrix = NULL;
  }//free2D
  </code></pre>
  
  <p>Any help or a start would be greatly appreciated.</p>