the link list structure is like that
(data|next)==>(data|next)==>(data|next)==>(data|ne xt) etc..
the is some node called elt its structure is:
(data|next)
if they would say
so the next of "elt" is node pCode:elt->next=p;
(data|next)==>(data|next)
but they say
so itsCode:elt->next=*p;
(data|next)==>(data)
??
i need an explanation that explains it in this simple way of schematics:
(data|next)==>(data|next)==>(data|next)==>(data|ne xt) etc..
Last edited by transgalactic2; 01-17-2009 at 11:09 AM.
So what part in this post don't you understand?
i dont understand how whould it change the schematics of the linked list if its
*p instead of p
??
can you please make a schematics explaining this
because things like
"address of the pointer to points to the Node" are very abstract
i cant imagine it.
can you say how would my linked list look like
after this code??
It's not very abstract.
First, malloc is called and the address where the allocation was made is stored in p.Code:int main() { int* p = malloc( sizeof(int) ); foo(&p); } void foo(int** p) { free(*p); }
Then the address of p (the address of the pointer!) is passed to foo, which then dereferences that pointer-to-pointer-to-int to get the address stored in p in main, and frees it.
Do you see the connection here?
p in foo is the address of the pointer to the int.
i understand every thing till this part
"which then dereferences that pointer-to-pointer-to-int to get the address stored in p in main, and frees it."
whats dereferces?? (i know that there is no reference in C?)
and i dont know what that means "pointer-to-pointer-to-int to get the address stored in p in main"
can you tell that in simpler words?
Dereference is the process called when you put the * before a pointer.
int* p;
*p = 5; // Dereferences p
>>pointer-to-pointer-to-int
int** basically. It is a pointer, which contains the address of another pointer (thus points to that pointer), and that pointer contains the address of the allocated int, it points to the int:
>>to get the address stored in p in mainCode:Pointer1: -> points to Pointer2 -> Pointer2: -> points to allocated int
Basically, what that means is that we get the value stored inside the pointer named p, which is defined inside main.
so derefences means that you put a value in the address of pCode:*p = 5; // Dereferences p
correct?
and int** means to use the next pointer of the linked list.
and in this part
you take a number only a number and put it as a parameter of a functionCode:foo(&p);
its like saying "use 5 as a pointer"
its just a number
you need to put there a name of a pointer
??
No, to dereference a pointer is to get what the pointer points to. In the example you get what the pointer points to and assign the value 5 to it.Originally Posted by transgalactic2
No, you take the address of the object, and pass it to the function named foo as an argument.Originally Posted by transgalactic2
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
what we do with this passed address?
where does it go??
It is copied to the function's argument, which is a pointer. After all, a pointer contains an address.Originally Posted by transgalactic2
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
No, no, no. Are you living under a rock?
Have you really no book or do you lack so much understanding of the language?
No, no, no.
Dereferencing means getting the value the pointer points at.
So the "*p" part is described as "dereference the pointer p."
Assigning a value is a separate step. Naturally, to assign a value where the pointer points, you must first dereference the pointer.
Look at it this way:
Every variable in a program has to be stored somewhere. So it is stored in memory. And to be able to access things in memory, everything must have an address.
Variables are automatically handled by the compiler, so you can set and get a value from them without trouble. It's transparent.
You can also figure out the address where variables are stored in memory. This "value" must be stored in a pointer.
A pointer is also a variable.
If you assign something to a pointer, you assign an address, because that's what pointers store.
You can also dereference a pointer, which basically means, get (or set) that value stored at the address stored inside this pointer.
Does that make sense?
int** means it's a pointer that stores addresses of int* pointers (pointers-to-int).and int** means to use the next pointer of the linked list.
This part takes the address of the local variable p, whose type is int*, and passes it along to the function foo, which expects the address of an int* variable (thus it takes an int** argument).and in this part
you take a number only a number and put it as a parameter of a functionCode:foo(&p);
its like saying "use 5 as a pointer"
its just a number
you need to put there a name of a pointer
??
How about this then?
If you can understand how that code works and why it needs to take pointer-to-pointers-to-int, then you have solved the mystery of your original code.Code:int main() { int* p; Allocate(&p); Destroy(&p); } void Allocate(int** p) { *p = malloc( sizeof(int) ); } void Destroy(int** p) { free(*p); *p = NULL; }
PS: Feel free to merge these posts if that is your wish. Yes, I'm talking to YOU, Moderators!