I could not find an errata for the 2nd edition of this book. My question concerns the if-statement in the following piece of code.
void removeHead (Node ** head) {
Node * temp;
if (!(*head)) {
temp = (*head)->next;
delete *head;
*head = temp;
}
}
So I understand that the point of the if-statement is to check if the Node is null. However by adding an additional "!" to the evaluation, wouldn't this negate the false value of null? Would it be correct to change it to something like:
if (*head) { ... }
Also if anyone knows where I can find an official errata for the 2nd edition that would be great.
Thanks,
Sam
The book is a little flawed in that it first claims that there are no problems with *head
as an input, and then further develops to say you should really pass in **head
, and then says you should check *head
. You really need to check *head
AND head
.
As for if (!(*head))
, if *head
is NULL, this would be if (!0)
which would be true. Then we try (0)->next
and die. Definitely a mistake.
The code as shown is incorrect: if *head
is null, you dereference it, which is certainly incorrect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With