由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 菜鸟问个C++问题
相关主题
ask a simple question about int pointer.C++ pointer problem
问个程序问题C++ interdependence question
Go adopts JavaScript’s idea of semicolon insertionfunction pointer 和 call-back function 有什么区别?
数组定义的时候,分配空间了么?C++里get array size的问题 (转载)
C里面一个被分配了内存的指针如何知道分配了多少?最新某公司onsite面试题 (转载)
在子函数内开内存,返回主函数指针然后释放空间是不是很糟糕的(转载)can you insert several function pointers into a queue ?
这个怎么allocate memory?关于C++中一个Class的大小 (转载)
difference between: char** p and char*p[] ??a weak c question, how to pass an array into a function?
相关话题的讨论汇总
话题: head话题: newelem话题: element话题: pointer话题: value
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
a weak c question, how to pass an array into a function?C里面一个被分配了内存的指针如何知道分配了多少?
问题:vptr/vtable for virtual function & vptr/vtable for在子函数内开内存,返回主函数指针然后释放空间是不是很糟糕的(转载)
about STL functor and function pointers这个怎么allocate memory?
c++ questiondifference between: char** p and char*p[] ??
ask a simple question about int pointer.C++ pointer problem
问个程序问题C++ interdependence question
Go adopts JavaScript’s idea of semicolon insertionfunction pointer 和 call-back function 有什么区别?
数组定义的时候,分配空间了么?C++里get array size的问题 (转载)
相关话题的讨论汇总
话题: head话题: newelem话题: element话题: pointer话题: value