I have written a piece of code to add nodes to the linked list .
Code:#include <stdio.h> struct node { int n; struct node *next; }typedef node_t; node_t *top = NULL; int main() { node_t *list = NULL; int choice; while(1) { printf("Please Enter your choice \n"); printf("1.Addition \n"); printf("2.Exit \n"); scanf("%d",&choxcice); switch(choice) { case 1: addition(&list); break; case 2: return(0); break; } } return 0; } int addition(node_t *list) { int number; node_t *newnode; printf("Take number \n"); scanf("%d",&number); newnode = malloc(sizeof(node_t) * 1); if (top == NULL) { newnode->n = number; newnode->next = NULL; top = newnode; list = newnode; } else { newnode->n = number; newnode->next = NULL; list->next = newnode; list = list->next; } return 0; }
Here my doubts are.
1) If i give "list" argument without & in addtion function , its showing some other behaviour where i cannot add to it, i think the pointer variable means itself address so why do again i need send address here?
2) after one iteriation , the list is becoming null , ( address it s showing in gdb after one node addition is NULL ).