Home > Back-end >  Pointer to a pointer, online, etc., as shown in the second floor
Pointer to a pointer, online, etc., as shown in the second floor

Time:11-12

 # include & lt; stdio.h> 
#include

Typedef struct _node
{
The int value.
Struct _node * next;
} Node;

Int main (void)
{
The head Node *=NULL;
For (int number; The scanf (" % d ", & amp; Number)==1 & amp; & Number!=1; )
{
The last Node * *=& amp; The head;
For (; * the last; The last=& amp; (* last) - & gt; Next);
(* last)=(*) malloc (sizeof (Node));
(* last) - & gt; Value=https://bbs.csdn.net/topics/number;
(* last) - & gt; Next=NULL;
}
For (Node * p=head; p; P=p - & gt; Next)
Printf (" % d ", p - & gt; Value);
return 0;
}

CodePudding user response:

For the first time to enter for (; * the last; The last=& amp; (* last) - & gt; Next); Due to the last point to the head, a head to NULL, so does not perform the for loop, the back of the statement for the head points to the first node Node1; For the second time in for (; * the last; The last=& amp; (* last) - & gt; Next) loop * last, that is, the head is pointing at the Node1, so Node1 will make the last point to the latest quarter, below 2 connection to the Node1, should not be (* last - next)=(*) malloc (sizeof (Node))?

CodePudding user response:

Don't understand why want to use the secondary pointer, there is only one main function is unnecessary in this way,

CodePudding user response:

Do you want to think about it, enter for (; * the last; The last=& amp; (* last) - & gt; Next); Exit the loop condition is what? Is * * last do not set up, the last==null, so when will * last==null? It is not * last=Node1. Next is null (because Node1 not null)? So (* last - & gt; Next)=malloc is wrong, because * last is null, * last - & gt; Next is wrong (null - & gt; Next), so, * last=malloc is right,