(nil) C/C++ 中的指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4744650/
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
(nil) pointer in C/C++
提问by CF711
I am working on a project and I keep coming across this error that will not allow me to complete the project. When I initialize one of my pointers to point to an object that will be made during the execution of the program and I initialize it to NULL
. Then when I check to see what it is set to it returns a value of nil
. How is such a thing possible? I didn't believe that nil pointers existed in C. Is there any way around this?
我正在处理一个项目,我不断遇到这个错误,这将不允许我完成该项目。当我将一个指针初始化为指向将在程序执行期间创建的对象时,我将其初始化为NULL
. 然后当我检查它的设置时,它返回一个值nil
. 这样的事情怎么可能?我不相信 C 中存在 nil 指针。有什么办法可以解决这个问题吗?
struct order_line *front = NULL;
...
printf("Head: %p\n", front); // prints -> Head: (nil)
回答by moinudin
%p
in printf
formats a pointer type. This is going to distinguish a null-pointer and print (nil)
because it is a special value in the context of a pointer. If you want to output 0 for a null pointer, cast the pointer to an integer and use %d
instead:
%p
在printf
格式中是一个指针类型。这将区分空指针和打印,(nil)
因为它是指针上下文中的特殊值。如果要为空指针输出 0,请将指针转换为整数并%d
改用:
printf("Head: %d\n", (int) front);
Original answer as it may still be useful:
原始答案,因为它可能仍然有用:
NULL
is a macro defined as 0
or ((void *) 0)
, so if you set a pointer to NULL
it's exactly the same as setting it to 0
. This works for the purposed of declaring null pointers because the memory at address 0 will never be allocated to your program.
NULL
是一个定义为0
or的宏((void *) 0)
,因此如果您设置指向NULL
它的指针与将其设置为0
. 这适用于声明空指针的目的,因为地址 0 处的内存永远不会分配给您的程序。
回答by dreamlax
When you print a pointer using printf("%p", somePtr)
, it is printed in an implementation-defined manner, as per this quote from the POSIX printf specification(similar wording exists in the C99 specification also).
当您使用 打印指针时printf("%p", somePtr)
,它以实现定义的方式打印,根据POSIX printf 规范中的引用(C99 规范中也存在类似的措辞)。
The argument must be a pointer to void. The value of the pointer is converted to a sequence of printable characters, in an implementation-dependent manner.
参数必须是指向 void 的指针。指针的值以依赖于实现的方式转换为可打印字符序列。
I guess, that this means if the pointer is NULL
, it may print it however it wants, including printing it as nil
or 0x00000000
or 0
.
我猜,这意味着如果指针是NULL
,它可以随意打印它,包括将其打印为nil
or0x00000000
或0
。
回答by user582912
use
用
printf("Head: %s, %d, %p\n", front, front, front);
to print Head: (null), 0, (nil)
打印 Head: (null), 0, (nil)
Thanks Packia
感谢 Packia
回答by rerun
I'm assuming that nil is what your debugger is telling you. In most compilers null is just #define ed to 0 anyway so that name is not that important.
我假设 nil 是您的调试器告诉您的。在大多数编译器中,null 只是 #define ed 为 0,因此名称并不那么重要。
回答by Donotalo
As others have stated, there is no such thing as nil pointer in C.
正如其他人所说,C 中没有 nil 指针这样的东西。
May be your memory allocation fails, causing 0
to be assigned to the pointer.
可能是你的内存分配失败,导致0
被分配给了指针。
回答by toofast1227
If your question is about comparing the pointer value to NULL then the value printed by printf() shouldn't matter. You can still do if(ptr==NULL)for that.
如果您的问题是关于将指针值与 NULL 进行比较,那么 printf() 打印的值应该无关紧要。你仍然可以这样做if(ptr==NULL)。
- ptr = (nil) and NULL = (nil) => ptr = NULL :)
- ptr = (nil) 和 NULL = (nil) => ptr = NULL :)
你得到的错误一定是因为其他原因。
回答by Rocky
After casting the structure pointer with int , it provides the expected condition with if statement ..
使用 int 转换结构指针后,它使用 if 语句提供预期条件 ..
printf("The address of the variable is---ps->p---After Deletion-- %p \n",ps->p);
printf("The address of the variable is---ps->p---After Deletion-- %d \n",(int)ps->p);
if((int)ps->p){
printf("Again in Free\n");
doFree((void **)&ps->p);
}
OUTPUT :-
输出 :-
The address of the variable is---ps->p---After Deletion-- nil The address of the variable is---ps->p---After Deletion-- 0
变量的地址是---ps->p---删除后--nil 变量的地址是---ps->p---删除后--0
it will evaluates to false for if condition .
对于 if 条件,它将评估为 false。