0

I have homework that I have to define a function this: void initialize_hotel(char**** hotel, int n, int m); I'm trying to understand what is four (*) and how to allocate the memory, n are rows and m is columns.

  • 1
    What data should go in the array? A `char****` (very rare!) is a pointer to a 3-dimensional stagged array. This really must be a homework question, since I have never used something like this in 20 years of professional programming. – PMF May 25 '21 at 12:54
  • Welcome to Stack Overflow. Do you understand `char*`? How about `char**`? Do you know how to construct a 2D array? If `n` is rows and `m` is columns, what is the third dimension? – Beta May 25 '21 at 12:55
  • 1
    `char**** hotel` is likely to be viewed as a pointer to a 2D array of pointers to strings. – chux - Reinstate Monica May 25 '21 at 12:57
  • @Lundin thanks God I do not work for `Signiel Seoul` which is the **seven** star hotel :) `void book(char *******hotel);` – 0___________ May 25 '21 at 13:20

2 Answers2

3

That prototype simply doesn't make any sense.

A pointer to pointer to-... -to pointer cannot point at an array. A pointer to pointer may point at a pointer. Which in some specific case might be the first element in an array of pointers... but in general that doesn't make any sense.

So either this is a trick question where the teacher tries to give you an impossible task to demonstrate how to not write programs. Or alternatively, your teacher is incompetent, let's not hope that's the case.

There's a common misunderstanding that pointer-to-pointer things are somehow how you allocate multi-dimensional arrays dynamically. In case of an array of strings with individual lengths allocated dynamically, a char** makes sense. In all other situations, pointer to pointer does not likely make sense, and especially not when you pile on even more * on top of it. If we want an array of arrays of strings, each string with individual length, that would be a char* [n][m] type, which has nothing to do with char***.

See Correctly allocating multi-dimensional arrays. From that post we can learn that one possible way to allocate a 3D array is this:

void arr_alloc (size_t x, size_t y, size_t z, int(**aptr)[x][y][z])
{
  *aptr = malloc( sizeof(int[x][y][z]) ); // allocate a true 3D array
  assert(*aptr != NULL);
}

Or in case that syntax feels too intimidating, simply:

void* arr_alloc (int x, int y, int z)
{
  return malloc( sizeof(int[x][y][z]) ); 
}

Or in the case of an array of arrays of strings with individual length (not something I'd recommend to use):

void* str_alloc (int x, int y)
{
  return malloc( sizeof(char*[x][y]) );
} 
Lundin
  • 195,001
  • 40
  • 254
  • 396
-1
#include <stdio.h>
#include <stdlib.h> // Header require for malloc(), function for memory allocation

// char * == string, char ** == ROW, char *** == COLUMN, char **** == table
void initialize_hotel(char ****hotel, int n, int m)
{
    *hotel = (char ***)malloc(sizeof(char ***) * m);
    for (int i = 0; i < n; i++)
        (*hotel)[i] = (char **)malloc(sizeof(char **) * n);
}

#define ROW 3 //name, address, price
#define COLUMN 2 // {{"Hilton", "1st Avenue", "$50"} {"Citi", "2nd Bell", "$80"} }

int main() {
        char ***table;
        initialize_hotel(&table, ROW, COLUMN);

        table[0][0] = "Hilton";
        table[0][1] = "1st Avenue";
        table[0][2] = "$50";

        table[1][0] = "Citi";
        table[1][1] = "2nd Bell";
        table[1][2] = "$80";

        for(int i = 0; i < COLUMN; i++){
            for(int ii = 0; ii < ROW; ii++){
                if(ii == 0)
                    printf("Name: ");
                if(ii == 1)
                    printf("Address: ");
                if(ii == 2)
                    printf("Price: ");
                printf("%s\n", table[i][ii]);
            }
            putchar('\n');
        }
}
Xenox Inno
  • 138
  • 1
  • 5