(nil) C/C++ 中的指针
我正在开发一个项目,并且不断遇到此错误,该错误不允许我完成该项目。当我初始化一个指针以指向将在程序执行期间创建的对象时,我将其初始化为NULL
。然后,当我检查它的设置时,它返回值 nil
。这样的事怎么可能呢?我不相信 C 中存在 nil 指针。有什么办法解决这个问题吗?
struct order_line *front = NULL;
...
printf("Head: %p\n", front); // prints -> Head: (nil)
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?
struct order_line *front = NULL;
...
printf("Head: %p\n", front); // prints -> Head: (nil)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
printf
中的%p
格式化指针类型。这将区分空指针并打印(nil)
因为它是指针上下文中的特殊值。如果您想为空指针输出 0,请将指针转换为整数并使用%d
代替:原始答案,因为它可能仍然有用:
NULL
是一个宏定义为0
或((void *) 0)
,因此,如果将指针设置为NULL
,则与将其设置为完全相同代码>0。这适用于声明空指针,因为地址 0 处的内存永远不会分配给您的程序。
%p
inprintf
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:Original answer as it may still be useful:
NULL
is a macro defined as0
or((void *) 0)
, so if you set a pointer toNULL
it's exactly the same as setting it to0
. This works for the purposed of declaring null pointers because the memory at address 0 will never be allocated to your program.用于
打印
Head: (null), 0, (nil)
谢谢
帕基亚
use
to print
Head: (null), 0, (nil)
Thanks
Packia
当您使用
printf("%p", somePtr)
打印指针时,它会以实现定义的方式打印,正如 POSIX printf 规范(C99 规范中也存在类似的措辞)。我猜,这意味着如果指针为 NULL,它可以按照需要打印它,包括将其打印为 nil 或 0x00000000 或 <代码>0。
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).I guess, that this means if the pointer is
NULL
, it may print it however it wants, including printing it asnil
or0x00000000
or0
.我假设 nil 是你的调试器告诉你的。在大多数编译器中,null 只是 #define 编辑为 0,因此名称并不那么重要。
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.
正如其他人所说,C 中不存在 nil 指针之类的东西。
可能是您的内存分配失败,导致
0
被分配给指针。As others have stated, there is no such thing as nil pointer in C.
May be your memory allocation fails, causing
0
to be assigned to the pointer.如果您的问题是将指针值与 NULL 进行比较,那么 printf() 打印的值应该无关紧要。您仍然可以为此执行if(ptr==NULL)。
ptr = (nil) and NULL = (nil) => ptr = NULL :)
The error you are getting must because of some other reason.
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.
ptr = (nil) and NULL = (nil) => ptr = NULL :)
The error you are getting must because of some other reason.
用 int 转换结构体指针后,它通过 if 语句提供预期的条件 ..
OUTPUT :-
变量的地址是---ps->p---删除后-- nil
变量的地址是---ps->p---删除后-- 0
对于 if 条件,它将计算为 false。
After casting the structure pointer with int , it provides the expected condition with if statement ..
OUTPUT :-
The address of the variable is---ps->p---After Deletion-- nil
The address of the variable is---ps->p---After Deletion-- 0
it will evaluates to false for if condition .