我很困惑动态内存分配和删除2D指针数组。目标是创建一个2D数组,其中每个单元格都有指向链表的指针。这就是我的做法,我没有发现任何错误,但有一些警告。
警告:
1)
2)
3)
以下是代码:
警告:
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()) ) {
以下是代码:
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);
}
我在以下几个方面感到困惑:
- 我不确定我的二维指针数组声明是否正确
- 如何传递这个二维指针数组
queue *(*arr)[COLUMNS] = malloc(sizeof(*arr) * ROWS);
? - user529758malloc()
?绝不可能。 - user529758arr
,它是指向queue
指针数组的指针。 - user529758