我买了这本书叫“Writing Solid Code”。
在第一章,有这样的代码:
while(*pchTo++ = *pchFrom++)
NULL;
我真的不明白NULL
那个循环中的作用。
我买了这本书叫“Writing Solid Code”。
在第一章,有这样的代码:
while(*pchTo++ = *pchFrom++)
NULL;
我真的不明白NULL
那个循环中的作用。
作者可以写一个完全空的while
循环,就像你可以写一个无体for
循环一样。那看起来像:
while (*pchTo++ = *pchFrom++);
/* or */
while (*pchTo++ = *pchFrom++)
;
对于某些人来说,这两者都可能看起来令人困惑,因此他们添加了 NULL 来赋予它一个主体并使其不那么混乱。
编辑
请注意,您可以对 for 循环执行相同操作:
for (head = list->head; head->next; head = head->next);
/* or */
for (head = list->head; head->next; head = head->next)
;
/* or */
for (head = list->head; head->next; head = head->next)
NULL;
所要做的就是通过转到下一个元素而不是下一个元素来遍历列表NULL
It's likely inspired by the syntax of Ada's null statement:
while condition loop
null;
end loop;
Ada uses the null
keyword both for the null pointer constant and for the null statement (and a few other things).
It does have some advantages over C's null statement, which is just a semicolon. In particular, it's much more explicit. A fairly common error in C code is to insert an accidental null statement by adding a semicolon, particularly among inexperienced C programmers who haven't yet worked out where semicolons are necessary and where they aren't:
while (condition);
{
/* statements */
}
Without the semicolon, the statements are controlled by the while loop. With it, the body of the while loop is empty, and the loop is likely be infinite if the condition has no side effects.
On the other hand, if you really want a null statement, using just a semicolon can leave the reader wondering if something was unintentionally left out.
In C, NULL
is a macro that expands to an implementation-defined null pointer constant. The author is using it here:
while(*pchTo++ = *pchFrom++)
NULL;
as a kind of null statement -- which actually works, because an expression followed by a semicolon is a statement expression in which the statement is evaluated for its side effects. Since it has no side effects, it does nothing; it acts just like a real null statement:
while(*pchTo++ = *pchFrom++)
;
Another equivalent form:
while(*pchTo++ = *pchFrom++)
42;
In my opinion, this is well intended but a bad idea. It's easily recognizable to those few of us who happen to be familiar with both C and Ada, but most experienced C programmers will look at it and wonder what the heck that null pointer constant is doing there.
It's not quite as bad as defining a set of macros to make C look like another language's syntax:
#define IF if (
#define THEN )
#define BEGIN {
#define END }
#define ELSE } else {
but it's in the same spirit.
My advice: Don't do this kind of thing. If you want your C code to be easily understandable by readers who know C, write idiomatic C code; don't invent clever tricks to make it look like something else. Null statements can be confusing, leading the reader to wonder if something was left out accidentally. The best solution that, IMHO, is to use a comment:
while(*pchTo++ = *pchFrom++) {
/* empty body */
}
该循环复制一个字符串。NULL
只是为了提供一个循环体。
此构造提供了一种在使用调试器时放置断点的好方法。一些 IDE 不允许我在循环上放置断点,而且我的循环体为空。如果您的循环体中有一条语句,则在其上放置断点会更容易,即使该语句无用。
否则,正如已经说过的,它应该做的事情与一个空体的身体完全一样。