7
In Code Review there is a implementation of a simply chained list that I am using.
However, in the reply of one of the users, he made some modifications to the list, and had a particular modification that left me with doubts.
The modification the user made was in the procedure add()
:
void add(struct LinkedList *list, int value) {
if(list->firstElement == NULL) {
struct Element *newElement;
newElement = malloc(sizeof(struct Element));
newElement->value = value;
newElement->nextElement = NULL;
list->firstElement = newElement;
} else {
struct Element *lastElement;
lastElement = list->firstElement;
while(lastElement->nextElement != NULL) {
lastElement = lastElement->nextElement;
}
struct Element *newElement;
newElement = malloc(sizeof(struct Element));
newElement->value = value;
newElement->nextElement = NULL;
lastElement->nextElement = newElement;
}
}
To:
void add(List* head, int value) {
while(*head)
head = &head[0]->next;
*head = malloc(sizeof **head);
if(!*head) abort();
**head = (Node){0, value};
}
And my doubt is in the statement while
and if
, which are declared as follows as shown below:
while(*head)
and:
if(!*head)
It’s the first time I’ve seen a statement from while
and if
like this one above, using pointer.
Question
I would like to know what these statements while(*head)
and if(!*head)
mean and what is the purpose of a statement of this type with pointer in flow control commands?