在最近的Slashdot Interview篇文章中,Linus Torvalds举了一个例子,说明一些人使用指针的方式表明他们并不真正理解如何正确使用指针.
不幸的是,由于我是他所说的人之一,我也未能理解他的例子:
I've seen too many people who delete a singly-linked list entry by keeping track of the "prev" entry, and then to delete the entry, doing something like个
if (prev) prev->next = entry->next; else list_head = entry->next;
and whenever I see code like that, I just go "This person doesn't understand pointers". And it's sadly quite common. People who understand pointers just use a "pointer to the entry pointer", and initialize that with the address of the list_head. And then as they traverse the list, they can remove the entry without using any conditionals, by just doing
*pp = entry->next
有人能再解释一下为什么这种方法更好,以及它如何在没有条件语句的情况下工作吗?