SortedInsert() is a function which given a list that is sorted in increasing order, and a
single node, inserts the node into the correct sorted position in the list.I know this code works fine.I am just curious to know why we can't use just single pointer to node to traverse the string.why can't we use struct node * currentref.Just explain me how this works.

void SortedInsert(struct node** headRef, struct node* newNode) {
struct node** currentRef = headRef;
while (*currentRef!=NULL && (*currentRef)->data<newNode->data) {
currentRef = &((*currentRef)->next);
newNode->next = *currentRef; // Bug: this line used to have
// an incorrect (*currRef)->next
*currentRef = newNode;

The function that calls the one you posted has to pass a pointer by reference, hence there are two stars not just one. If it did not do that then SortedInsert() would be unable to change the linked list declared in the calling function.

To further develop AD's point

void func1(int** p)
        int x=20;
        *p= &x;
        printf("In pointer to pointer to int\n");

void func(int* p)
        int x=20;
        p= &x;
        printf("In pointer to int\n");


int main()
        int x=10;
        int* p= &x;
        printf("Value of x now is %d\n",*p);
        printf("Value of x now is %d\n",*p);

        return 0;

Maybe this example will clear your confusion

@AD,abhimanipal I know that but my question is why we are using struct node **currentRef .Why can't i use struct node *currentRef to traverse the string and add it.Because suppose i need to add a node to the linked list as this code shows

void add(Node * head,int val)           //here Node is struct node *
*head=(Node)malloc(sizeof(struct node));
Node current =*head;
current->next=(Node)malloc(sizeof(struct node));

This code generates correct output whereas for the code posted before it is giving error when i replace node ** currentref with node *currentref

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

Both the codes which i have posted works fine.My question is for first one i traverse the list using double pointer and in second i traverse using single pointer.why is it like this?
typedef struct node *Node;

This article has been dead for over six months. Start a new discussion instead.