1

I have always read that in order to modify any variable inside a function in C, you must pass a pointer to that variable.

If I want to delete a particular element in a linked list, and I do something like:

int DeleteElement(element **head, element *deleteMe)
{
  free(deleteMe);
  return 1;
}

Why am I able to free that deleteMe and that gets reflected outside the DeleteElement function? Isn't the deleteMe another thing inside the function?

Thanks

4

2 回答 2

0

您没有向指针写入任何内容deleteMe。您正在使用它(以只读方式)来确定要释放的对象(它指向的对象)。

于 2013-01-20T14:33:50.297 回答
0

您将指针传递给地址(用于提供该指针的名称或变量无关紧要)。该地址的内存块被释放。因此,它也在DeleteElement函数“外部”被释放。

于 2013-01-20T14:34:01.750 回答