0

So there are 2 structs:

struct Morning { 
    int time;                        
    int day;                 
    struct Morning *next;   //pointer for the next node if there are any collisions     
};

struct Days_Hash_Table {
    int count;                     
    struct Morning **task; // array for the hash table
};

How can I allocate memory for the struct Morning **task? Also, how can I define the array size ?(the size is always stored in a global variable, say array_size.) I tried the following:

struct Days_Hash_Table* table = malloc(sizeof(struct Days_Hash_Table)+ sizeof(struct Morning)*array_size);

and when I tried accessing the array, for example, table->task[0]->time = 0; I got segmentation fault. What is the right way of approaching this? Also will it be easier if I change **task to *task[]?

Thanks!

koozyy
  • 23
  • 2
  • You need to first decide whether your table elements are pointers or structures. The `struct` defines an array of pointers but your allocation is for an array of structures. For a hash table I would suggest you need an array of pointers. If that is the case you need to allocate the table and then you need seperate allocations for the structures when adding elements to the hash table. – kaylum Dec 21 '20 at 01:17

1 Answers1

0

If you want to allocate the way you show you need to declare it as:

struct Days_Hash_Table {
    int count;                     
    struct Morning task[]; 
};

and allocate:

struct Days_Hash_Table* table = malloc(sizeof(*table)+ sizeof(table -> task[0])*array_size);

EDIT

struct Morning { 
    int time;                        
    int day;                 
    struct Morning *next;   //pointer for the next node if there are any collisions     
};


struct Days_Hash_Table {
    int count;                     
    struct Morning task[]; 
};


struct Days_Hash_Table*alloc(size_t array_size)
{
    struct Days_Hash_Table* table = malloc(sizeof(*table)+ sizeof(table -> task[0])*array_size);

    if(table)
    {
        for(size_t index = 0; index < array_size; index++)
        {
            table -> task[index].time = index + 1;
            table -> task[index].day = index + 100;
        }
    }
    return table;    
}

int main(void)
{
    struct Days_Hash_Table* table = alloc(20);

    if(table)
    {
        for(size_t index = 0; index < 20; index++)
        {
            printf("Time[%zu] = %d ", index, table -> task[index].time);
            printf("Day[%zu] = %d\n", index, table -> task[index].day);
        }
    }
    free(table)
;}

https://godbolt.org/z/e66vzq

0___________
  • 60,014
  • 4
  • 34
  • 74
  • did you mean to declare it as `struct Morning *task[];`? also after declaration , I do `table->task[0]->time = 2; ` and it gets seg fault. why is that so? – koozyy Dec 21 '20 at 13:13
  • did I declare it this way? I think you need to learn a bit the C language. https://godbolt.org/z/e66vzq – 0___________ Dec 21 '20 at 16:03
  • oh okay, I actually need to keep the format of the structs as it is, because it''s a project. Thanks for the answer and for the link though – koozyy Dec 21 '20 at 20:03