C语言 如何检查void指针是否指向NULL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18296117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if void pointer points to NULL?
提问by pyrrhic
So if you do:
所以如果你这样做:
void *ptr = NULL;
What is the best way to check if that void pointer is NULL?
检查该空指针是否为 NULL 的最佳方法是什么?
My workaround for now is this:
我现在的解决方法是这样的:
if (*(void**)ptr == NULL) ...
But this doesn't seem like the best way, as I'm implicitly assuming ptr is of type void** (which it isn't).
但这似乎不是最好的方法,因为我隐含地假设 ptr 的类型为 void** (它不是)。
回答by nmaier
I'd simply write if (!ptr).
我只想写if (!ptr).
NULLis basically just 0and !0is true.
NULL基本上是公正的0,!0是真实的。
回答by HymanCColeman
Be sure to include a definition of NULL.
确保包含 NULL 的定义。
#include <stddef.h>
void *X = NULL;
// do stuff
if (X != NULL) // etc.
If you include <stdio.h>and similar then stddef.hgets pulled in automatically.
如果您包含<stdio.h>和类似的内容,则会stddef.h自动被拉入。
Finally, it is interesting to look at the definition of NULL in stddef.hand by doing this you will see why your initial guess at what to do is interesting.
最后,看看 NULL 的定义是很有趣的stddef.h,通过这样做,你会明白为什么你最初对做什么的猜测很有趣。
回答by verbose
A NULL pointer is a pointer that isn't pointing anywhere. Its value is typically defined in stddef.has follows:
NULL 指针是不指向任何地方的指针。它的值通常定义stddef.h如下:
#define NULL ((void*) 0)
or
或者
#define NULL 0
Since NULL is zero, an ifstatement to check whether a pointer is NULL is checking whether that pointer is zero. Hence if (ptr)evaluates to 1 when the pointer is not NULL, and conversely, if (!ptr)evaluates to 1 when the pointer is NULL.
由于 NULL 为零,因此if检查指针是否为 NULL的语句正在检查该指针是否为零。因此if (ptr),当指针不为 NULL 时计算结果为 1,相反,if (!ptr)当指针为 NULL 时计算结果为 1。
Your approach if (*(void**)ptr == NULL)casts the voidpointer as a pointer to a pointer, then attempts to dereference it. A dereferenced pointer-to-pointer yields a pointer, so it might seem like a valid approach. However, since ptris NULL, when you dereference it, you are invoking undefined behavior.
您的方法if (*(void**)ptr == NULL)将void指针转换为指向指针的指针,然后尝试取消引用它。取消引用的指针到指针会产生一个指针,因此这似乎是一种有效的方法。但是,由于ptrNULL,当您取消引用它时,您正在调用未定义的行为。
It's a lot simpler to check if (ptr == NULL)or, using terse notation, if (!ptr).
检查要简单得多,if (ptr == NULL)或者使用简洁的表示法,if (!ptr).
回答by xt454
If your code manages to compile when assigning void *ptr = NULL, then it stands to reason that a simple ifstatement to compare if it is NULLshould suffice, particularly because NULLwould have to be defined if the code can compile.
如果您的代码在分配时设法编译void *ptr = NULL,那么有理由if比较简单的语句NULL是否足够,特别是因为NULL如果代码可以编译就必须定义。
Example of sufficient way to check:
充分检查方法的示例:
if(ptr==NULL)
{
rest of code...
}
I wrote a little test program, compiled with gcc on linux, which works:
我写了一个小测试程序,在 linux 上用 gcc 编译,它可以工作:
int main()
{
void *ptr = NULL;
if(ptr==NULL)
{
return 1;
}
return 0;
}
回答by SajithP
I know this is a bit old post, but wanted to add something that might be useful.
我知道这是一个有点旧的帖子,但想添加一些可能有用的东西。
What I usually do is something like this,
我通常做的事情是这样的,
This is my function.
这是我的职能。
void MyMethod( const void* l_pData ///< Source data
, size_t l_nLen /**< Number of bytes to convert */) {
// Return if nothing is provided
if (l_pData == NULL || ((const char*)(l_pData))[0] == ' - Null data
- Empty data
- Invalid length
' || 0 == l_nLen) {
return;
}
// Rest of the code
}
You can check for
你可以检查
MyMethod("", 10);
MyMethod(" ", 10);
MyMethod(NULL, 10);
MyMethod("valid", 0);
Following are validated
以下经过验证
##代码##
