windows.h头文件定义了NULL吗?

huangapple go评论58阅读模式
英文:

Which windows.h header defines NULL?

问题

在某个时候,我需要在Windows代码中检查NULL,但我不想包含stdio.hstdlib.h

我强烈怀疑NULL在windows.h的某个地方被定义,但我找不到页面。我找到了这个,这很有趣,但没有告诉我我想要知道的信息。

英文:

I have to write some Windows code, and I want to separate it from the Linux part as much as possible.

At one point I need to check for NULL in the Windows code, but I don't want to include stdio.h or stdlib.h.

I strongly suspect that NULL is defined somewhere in windows.h, but I can't find the page. I found this, which is interesting, but doesn't tell me what I want to know.

答案1

得分: 8

NULL 在标准的 C 头文件 stddef.h 中定义,句号。

如果你在 Windows 上运行例如 gcc/mingw 端口,你只需告诉任何半靠谱的 IDE 找到 NULL 的声明,并最终进入 stddef.h,其中写着 #define NULL ((void *)0)

你还可以创建一个这样的源文件:

// main.c
#include <windows.h>
int main (void)
{}

然后使用 gcc main.c -H 进行编译。这将展开所有头文件的依赖关系,这样你就会看到哪个头文件包含了哪些其他头文件。你会得到一大堆头文件,并且你会注意到 stddef.h 间接地包含在大约 2-3 个不同的位置。

结论:NULL 不是由 windows.h 或任何其他应该直接包含的 Windows 特定头文件定义的。

如果你需要使用 NULL,则正确的方法是 #include <stddef.h>,不论操作系统如何。

英文:

NULL is defined in the standard C header stddef.h, period.

If you run for example gcc/mingw port in Windows, you can just tell any half-decent IDE to find the declaration of NULL and end up in stddef.h where it says #define NULL ((void *)0).

You can also create a source file like this:

// main.c
#include &lt;windows.h&gt;
int main (void)
{}

Then compile with gcc main.c -H. This will expand all header dependencies, so you'll see which header that includes what other headers. You'll get a whole flood of them and you'll notice that stddef.h is indirectly included at some 2-3 different locations.

Conclusion: NULL is not defined by windows.h or any other windows-specific header that you should be including directly.

If you need to use NULL, then the correct approach is to #include &lt;stddef.h&gt; regardless of OS.

huangapple
  • 本文由 发表于 2023年2月10日 15:41:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定