这是代表公司帮我同时file eb2 和eb3了么?# EB23 - 劳工卡
r*h
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;
}
不太明白里面讲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;
}