Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointer to pointer 1

Status
Not open for further replies.

tamkash

Programmer
Nov 7, 2007
5
CA
I have a question about the node pointer which i got from website.

void main()
{
node *front;
node *rear;
void qinsert(&front, &rear);
}
void qinsert(node **front1,node **rear1)
{
node *newnode; /* New node to be inserted */
newnode=(node*)malloc(sizeof(node));
newnode->next=NULL;
printf("\nEnter the character to push\n");
fflush(stdin);
scanf("%c",&(newnode->data));
if(*front1==NULL && *rear1==NULL)
{
*front1=newnode;
*rear1=newnode;
}
else
{
(*rear1)->next=newnode;
*rear1=newnode;
}
}

I know that address of front and rear is passed to qinsert function. In the qinsert function we create node *newnode and newnode is a pointer to the block of memory after malloc is initialized. What is the value of *front1 and *rear1 in the if and else condition?

Many thanks
 
Well, qinsert function must modify your list presented by head and tail pointers (to node, of course;). So you must pass not head/tail values but pointers to these pointer variables (to modify them via these pointers).
We have two pointer variables, for example:
Code:
node* listhead; /* initialize it before use, 0 at start */
node* listtail; /* -*-*- */
... /* Now we want more nodes and ask qinsert make node
       and fill our head/tail pointer variables... */
qinsert(&listhead,&listtail);/*pass addresses, not values*/
Pointer to pointer to node has node** type. if you want to check the value addressed by pointer, use * operation as in if statement in the snippet above:
Code:
    if(*front1==NULL && *rear1==NULL)
/* if the pointer addressed by the 1st parameter is NULL
   (empty list) and (redundance check;) the pointer
    addresses by the 2nd parameter is NULL then
    (my list is empty so I allocate node and pass
     its address in the list head and tail pointer
     variables accessed via its pointers)...
*/
 
The else condition code is adding another node to the end of the list.

Continuing ArkM's example, listtail is pointing to the last node, for arguments sake call it node3. Each node in the list has a pointer to the next node in the list.

So with node3 being the last node, a call is made to qinsert() a new node is malloc'd (say node4) and as neither front1 or rear1 are NULL it falls into the else case. Here, rear1 still points to node3 and so node3's 'next' pointer is set to point to the new node4. Now node3 is not the tail of the list, it's now node4 so rear1 is set to point to the new end of list.

front1 remains unchanged as the first node in the list hasn't changed.

Bertha
 
Easiest way to deal with pointers is to draw boxes with arrows. You'll soon get the picture and everything will be crystal clear.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top