i understand that dp points to sp
dp==>sp==>node
what exactly this expression means
*dp == sp
*dp is the derefencing form
so *dp is the address of pointer sp
correct?
i understand that dp points to sp
dp==>sp==>node
what exactly this expression means
*dp == sp
*dp is the derefencing form
so *dp is the address of pointer sp
correct?
so what this function does is building some pointer to node variable called elt.Code:typedef struct node { int value; struct node *next; }Node; void what1(Node ** p,int num){ Node *elt; elt=(Node*)malloc(sizeof(Node)); elt->next=*p; elt->value=num; *p=elt; }
then elt->next get to be pointed at the pointer which "p" points to
elt->value gets the "num"
and then the pointer which "p" pointed to ,points at the elt pointer??
but its not write
because by your definition p pints to a pointer
but *p must point a node
but it points at a pointer
so by this operation
they transform p from being pointer to pointer to nodeCode:*p=elt;
into pointer to pointer to pointer to node
correct?
Last edited by transgalactic2; 01-18-2009 at 10:49 AM.
Half of the time, I don't even know what you're talking about...
elt = new nodeCode:typedef struct node { int value; struct node* next; } Node; int main() { Node* pFirstNode = malloc( sizeof(Node) ); pFirstNode->value = 0; pFirstNode->next = NULL; what1(&pFirstNode, 10); } void what1(Node** p, int num) { Node* elt; // Define a new empty pointer elt = (Node*)malloc( sizeof(Node) ); // Allocate a new node, store in elt elt->next = *p; // Put the address stored in pFirstNode in main inside elt->next elt->value = num; // Store num in elt->value *p = elt; // Assign the address of the new node to pFirstNode in main. }
p -> pFirstNode -> Node
Freeing left out for simplicity.
Now let's see you figure out how or why.
As I said, half of the time, I don't know what you are writing.
That part is correct from what I can assume, if I understand correctly.
The word is "right", not "write".but its not write
And yes, it is right.
p points to a pointer, yes, and no, *p must not point to a node. p points to a pointer, Node*, so *p is a Node*.because by your definition p pints to a pointer
but *p must point a node
Absolutely not. What makes you think that?but it points at a pointer
so by this operation
they transform p from being pointer to pointer to nodeCode:*p=elt;
into pointer to pointer to pointer to node
correct?
"p" is not transformed at all.
What p is pointing to is assigned the address of the new node which elt points at.
Have you actually considered that C may not be for you?
You seem to have an awful lot of problems, never able to do it right, even the simplest of things.
The heart of your misunderstanding is in red. By assigning a pointer the same value as another pointer, they point to the same place:
Would be a circular linked list with two members. You can swap pointers around as much as you like:Code:node *p, *elt=malloc(sizeof(node)); p=elt; elt->next=p;
They all still point to a node, not to a pointer to a pointer to a pointer to a node or something.Code:elt=p; p->next=elt->next; elt->next=p->next; p=elt; p=p->next;
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
the pointer p the "head" points to *p which points to a node
correct?
but when we do
p is still the head ,but now p points to elt (the new *p)Code:*p=elt;
correct?
but in this case
will mean that p will point to elpCode:node **p, *elt=malloc(sizeof(node)); *p=elt;
because p points to *p so if our new *p is elp
then p points to elp
correct?
No, that results in undefined behaviour. p does not point to anything, so *p is a pointer that does not exist. Other than that, yes, p points to elt.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i thought that when we define
Node **p;
that means that p points to another pointer (*p) and this (*p) points to a node
where is my mistake?
Okay, you are wanting to use pointers to pointers, so I guess in this case:
p is a pointer to a pointer, however it cannot be a pointer to a node, so you cannot access the members of a node using *p (there is no *p->next). However, if you cast it as a character block, (char*)*p you can accesses the contents of the node pointed to by the pointer *p points to byte by byte.Code:node **p, *elt; *p=elt;
I haven't seen pointers to pointers performing a useful purpose in linked lists tho, so you are kind of pouring pickled pepper lemon juice in your milk here. I could be wrong.
Last edited by MK27; 01-18-2009 at 01:27 PM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
It is correct to say that since p is a pointer to a pointer it cannot be a pointer to a node, since a node is not a pointer. However, it is not correct to say that one cannot access the members of a node via p, since *p is a pointer to a node, hence (*p)->next is valid (assuming that p actually points to an existing pointer to a node, rather than to nothing as is the case at the moment).Originally Posted by MK27
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Okay. Maybe the reason I haven't tried that is because this:
Actually causes a segfault on my system. I'm guessing that transgalactic2 is not actually trying any code to see what it does before (s)he asks what it could mean.Code:node *test=malloc(sizeof(node)), **p; *p=test;
I can do this:
which I imagine is not the same thing, but then (*p)->next causes a segfault.Code:node *test=malloc(sizeof(node)), **p; p=&test;
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
so you agree with me that if we define
Node **p;
then we have two pointers in this definition
in which
p points *p
correct?