r***h 发帖数: 70 | 1 在看Programming interview exposed
不太明白里面讲link list插入新head的程序的例子.给出2个例子,书上说第一个是错的
,第二个才对.不明白为什么必须用**head而不是*head,难道head本身做为pointer pass
给函数不改变指针的内容吗? 请帮忙讲解一下,谢谢!
For example, the following code is incorrect because it fails to update the
head pointer in the calling function:
int BadInsert(element *head)
{
element *newElem;
newElem = (element *) malloc(sizeof(element));
if (!newElem) return 0;
newElem->next = head;
head = newElem;
return 1;
}
The correct way to update the head pointer in C is to pass a pointer to the
head pointer, allowing you to modify the calling function's pointer to the
first element, as shown here:
int Insert(element **head) {
element *newElem;
newElem = (element *) malloc(sizeof(element));
if (!newElem) return 0;
newElem->next = *head;
*head = newElem;
return 1;
} | j*a 发帖数: 14423 | 2 int main()
{
element *head;
BadInsert(head);
GoodInsert(&head);
}
pass
the
【在 r***h 的大作中提到】 : 在看Programming interview exposed : 不太明白里面讲link list插入新head的程序的例子.给出2个例子,书上说第一个是错的 : ,第二个才对.不明白为什么必须用**head而不是*head,难道head本身做为pointer pass : 给函数不改变指针的内容吗? 请帮忙讲解一下,谢谢! : For example, the following code is incorrect because it fails to update the : head pointer in the calling function: : int BadInsert(element *head) : { : element *newElem; : newElem = (element *) malloc(sizeof(element));
| f******y 发帖数: 2971 | 3 bad insert gets a local copy of the head, and changes the local copy only.
The value in the calling function remains the same. With **, you get the
address of the outside pointer, and change the value in that address, so the
outside head pointer is changed.
pass
the
【在 r***h 的大作中提到】 : 在看Programming interview exposed : 不太明白里面讲link list插入新head的程序的例子.给出2个例子,书上说第一个是错的 : ,第二个才对.不明白为什么必须用**head而不是*head,难道head本身做为pointer pass : 给函数不改变指针的内容吗? 请帮忙讲解一下,谢谢! : For example, the following code is incorrect because it fails to update the : head pointer in the calling function: : int BadInsert(element *head) : { : element *newElem; : newElem = (element *) malloc(sizeof(element));
| X****r 发帖数: 3557 | 4 A function does not change its arguments.
e.g.
void f(int x) {
x = 1;
}
void g(int* x) {
*x = 1;
}
int i = 0;
f(i);
// i is still 0 here.
g(&i);
// i is 1 now.
Same thing for linked list:
element *head = some_value;
BadInsert(head);
// head is still the same some_value
Insert(&head);
// now head contains the new value.
pass
the
【在 r***h 的大作中提到】 : 在看Programming interview exposed : 不太明白里面讲link list插入新head的程序的例子.给出2个例子,书上说第一个是错的 : ,第二个才对.不明白为什么必须用**head而不是*head,难道head本身做为pointer pass : 给函数不改变指针的内容吗? 请帮忙讲解一下,谢谢! : For example, the following code is incorrect because it fails to update the : head pointer in the calling function: : int BadInsert(element *head) : { : element *newElem; : newElem = (element *) malloc(sizeof(element));
| r***h 发帖数: 70 | 5 那个function按传递指针的看的懂.下面的例子也知道.
element *head = some_value;
BadInsert(head);
// head is still the same some_value
Insert(&head);
// now head contains the new value
但问题是为啥head必须用2个*,我按下面这么理解吗?
element **head = some_value;
BadInsert(*head);
// head is still the same some_value
Insert(head);
// now head contains the new value
the
【在 f******y 的大作中提到】 : bad insert gets a local copy of the head, and changes the local copy only. : The value in the calling function remains the same. With **, you get the : address of the outside pointer, and change the value in that address, so the : outside head pointer is changed. : : pass : the
| d****n 发帖数: 1637 | 6 **head is type of element **
point to a space with type element *
when you pass element ** , this double pointer not get modified, but the
space it is pointing to , element * get modified.
Therefore you need malloc for **head before call the function. otherwise, it
will be segmentation fault.
【在 r***h 的大作中提到】 : 那个function按传递指针的看的懂.下面的例子也知道. : element *head = some_value; : BadInsert(head); : // head is still the same some_value : Insert(&head); : // now head contains the new value : 但问题是为啥head必须用2个*,我按下面这么理解吗? : element **head = some_value; : BadInsert(*head); : // head is still the same some_value
|
|