Are NUL and NULL interchangeable in C? [duplicate]

旧巷老猫 提交于 2019-12-01 14:38:39

问题


Just read pointer on C book by Kenneth Reek. It says that a NUL byte is one whose bits are all 0, written like this '\0' and NULL has a value 0 which refers to a pointer whose value is zero.

Both are integers and have the same value, so they could be used interchangeably.

and this code also:

char const *keyword[] = { "do", "for", "if", "register", 
                              "return", "switch", "while", NULL };

In this code, the NULL lets functions that search the table detect the end of the table. So, can we interchange NULL and NUL macros?


回答1:


NUL is the notation for an ASCII character code 0.

NULL is a macro defined in stddef for the null pointer.

So, no, they are not to be used interchangeably.




回答2:


There is no standardized NUL macro, at least not in C.

Also, NULL has the advantage over a plain 0 literal that it signals to the reader that "hey, we're dealing with a pointer" which might be considered an advantage.

In C++, the idiom was for a long while to just write 0, but then they seem to have reversed that and introduced a new literal, nullptr for some reason(s).

In C, I recommend writing NULL for pointers and '\0' for characters.



来源:https://stackoverflow.com/questions/15479335/are-nul-and-null-interchangeable-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!