for example
in this code why do i have to use **p?cant it be *p??Code:createnode(mynodetype **p) { *p = (mynodetype * )malloc(sizeof(mynodetype)); *p ... main() { mynodetype **a; createnode(a); }
for example
in this code why do i have to use **p?cant it be *p??Code:createnode(mynodetype **p) { *p = (mynodetype * )malloc(sizeof(mynodetype)); *p ... main() { mynodetype **a; createnode(a); }
you should be doing
Otherwise you will be dereferencing an uninitialized memory address.Code:createnode(mynodetype **p) { *b = (mynodetype * )malloc(sizeof(mynodetype)); *b ... main() { mynodetype* a; createnode(&a); }
but why do we use **p rather than *p
Because p is a pointer to a pointer to a mynodetype. Dereferencing it once will give you a pointer to a mynodetype, while dereferencing it twice will give you the mynodetype being pointed to by the pointer whose memory address is stored in p.
thx for ur answer
Why not use:
?Code:CreateNode(MyNodeType *p) { p = (MyNodeType *)malloc(sizeof(MyNodeType)); } main() { MyNodeType *p; CreateNode(p); return 0; }
You use a pointer to a pointer when there is the potential need to update what you're originally pointing to. Consider:Originally posted by Xei
Why not use:
[--snip--]
?
You can now pass this function an empty list and have it update the original pointer. Without a pointer to a pointer, all you can do is something like:Code:struct node *list; void myFun( struct node ** listptr ) { ...do stuff... *listptr = newnode; } mFun( &list );
list = myFun2( ... );
Where myFun2 returns the pointer.
Quzah.
Hope is the first step on the road to disappointment.
Because that would cause a memory leak and you'd lose the memory location of the allocated memory once you leave the function. p in main would not be pointing to the newly allocated memory. You have to pass p by reference via a pointer to a pointer as was shown in example.Originally posted by Xei
Why not use:
?Code:CreateNode(MyNodeType *p) { p = (MyNodeType *)malloc(sizeof(MyNodeType)); } main() { MyNodeType *p; CreateNode(p); return 0; }
The original code is being used wrongly.
If you want change where a pointer is pointing by use of a function, there are two ways to do it nl.
1. Pass the pointer by reference.
2. Return the new pointer from the function.
Example:
In the original code, the function accepted a "**", and a "**" was passed to it. So the new pointer must either be returned or the pointer must be passed by reference.Code:#include <stdio.h> #include <stdlib.h> void createnode1(int ***p) { *p = (int**)malloc( sizeof(int*) ); } int** createnode2(int **p) { p = (int**)malloc( sizeof(int*) ); return p; } int main(int argc, char* argv[]) { int **a = NULL; int **b = NULL; printf("a = %p\n", a); createnode1(&a); printf("a = %p\n", a); printf("b = %p\n", b); b = createnode2( b ); printf("b = %p\n", b); free(a); free(b); fgetc(stdin); return 0; }
This seems very mentally taxing at the moment...(either that, or it's the 5.5 hours I just spent in a small room with 40 people learning NOTHING just to get a insurance discount). I shall find more material explaining this, if you know of any links, please post them. Thanks.
edit: nm, I get it, I'm just not thinking.
Last edited by Xei; 04-13-2003 at 05:00 PM.