猪猪虾 发表于 2022-3-15 22:36:20

怎么理解结构体数组里面的释放内存的函数

怎么理解这个函数 ReleaseTable(Table);,传参数的时候传的是Table,
而函数声明的时候ReleaseTable(struct Node** Ptable),传入的是双重指针


void ReleaseTable(struct Node** Ptable)
{
        struct Node* temp;
        for (int i = 0; i < Size; i++)
        {
                if (*Ptable != NULL) //*Ptable这里是解引用,对应第一个节点的地址
                {
                        temp = *Ptable;
                        *Ptable = (*Ptable)->next;
                        free(temp);
                }
        }
}

int main()
{
        struct Node* Table;
        struct Book* ptr = NULL;
        initialTable(Table);
        ReleaseTable(Table);
        return 0;
}

xieglt 发表于 2022-3-15 22:59:39

Table 的定义是   struct Node * Table;
这是一个指针数组,实际上与struct Node * * 是相同的数据类型,
所以,是可以当作实参传递给 ReleaseTable 函数的,编译可以通过,没有问题。


大马强 发表于 2022-3-15 23:02:18

我的理解是
struct Node* Table;
是一个指针数组,存放的是 struct Node*类型的指针
Table 作为数组名同时也是数组的首地址,而它存放的的是struct Node*的地址

所以在函数中
Ptable 表示存放struct Node*类型变量的地址的地址
*Ptable 表示存放struct Node*类型变量的地址

jhq999 发表于 2022-3-16 08:40:09


void ReleaseTable(struct Node** Ptable)
{
      struct Node* temp;
      for (temp=*Ptable->next;*Ptable != NULL;temp = *Ptable->next; )
      {
                        free(*Ptable);
                        *Ptable=temp;               
      }
}
页: [1]
查看完整版本: 怎么理解结构体数组里面的释放内存的函数