I am getting super confused with dynamic memory allocation and deletion of a 2D array of pointers.
The goal is to have a 2D array in which each cell has a pointer to a linked list. This is what I am doing, and I don't see any errors but few warnings.
Warnings :
1)
a value of type queue ** cannot be used to initialize an entity of type queue ***
queue* (**table) = (queue**)malloc(sizeof(queue*)*3);
2)
a value of type queue * cannot be assigned to an entity of type queue **
table[indexI] = (queue*)malloc(sizeof(queue*)*3);
3)
a value of type queue ** cannot be assigned to an entity of type queue ***
if( !(table = allocate()) ) {
Here is the code:
queue **allocate() {
queue* (**table) = (queue**)malloc(sizeof(queue*)*3);
// Warning #1 at above line
for(.....) {
table[index] = (queue*)malloc(sizeof(queue*)*3);
// Warning #2 at above line.
}
for(I index - 0 to 3) {
for(J index - 0 to 3) {
table[I][J] = NULL;
}
}
return((queue**)table);
}
void deallocate(queue* **table) {
// will handle list deletion
// next deallocate table
for(....) {
free(table[index]);
}
free(table);
}
void
add_list_to_queue(queue ***table) {
// here I create a list of queue type and assign it to
// those cells
}
modify_table() {
queue* (**table) = NULL;
table = allocate();
// Warning #3 at above line
.
.
.
add_list_to_queue(table);
// do de allocation of table, list etc.,
deallocate(table);
}
I have confusion in these areas
- I am not sure if my declaration of 2D array of pointers is correct
- How can I pass around this 2D array of pointers