I'm making a program to add items to a LIFO (last in first out) array, where the elements are inserted at the start of the array.
Here is my code:
typedef struct neud{
int num;
struct neud *next;
}neud;
void add(neud** head,int val){
//creat a new neod
neud* newNeod;
newNeod = malloc(sizeof(neud));
//assigning
newNeod->num = val;
newNeod->next= *head;
//change the head;
*head = newNeod;
}
int main()
{
neud* head = NULL;
add(&head,10);
add(&head,20);
add(&head,30);
return 0;
}
Everything works fine, but I don't understand precisely the need for a double-pointer here. Can someone explain this?