(nil) C/C++ 中的指针

发布于 2024-10-12 22:47:14 字数 289 浏览 14 评论 0原文

我正在开发一个项目,并且不断遇到此错误,该错误不允许我完成该项目。当我初始化一个指针以指向将在程序执行期间创建的对象时,我将其初始化为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

全部不再 2024-10-19 22:47:14

printf 中的 %p 格式化指针类型。这将区分空指针并打印 (nil) 因为它是指针上下文中的特殊值。如果您想为空指针输出 0,请将指针转换为整数并使用 %d 代替:

printf("Head: %d\n", (int) front);

原始答案,因为它可能仍然有用:

NULL 是一个宏定义为 0((void *) 0),因此,如果将指针设置为 NULL,则与将其设置为 完全相同代码>0。这适用于声明空指针,因为地址 0 处的内存永远不会分配给您的程序。

%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:

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.

二手情话 2024-10-19 22:47:14

用于

printf("Head: %s, %d, %p\n", front, front, front);

打印 Head: (null), 0, (nil)

谢谢
帕基亚

use

printf("Head: %s, %d, %p\n", front, front, front);

to print Head: (null), 0, (nil)

Thanks
Packia

好久不见√ 2024-10-19 22:47:14

当您使用 printf("%p", somePtr) 打印指针时,它会以实现定义的方式打印,正如 POSIX printf 规范(C99 规范中也存在类似的措辞)。

参数必须是指向 void 的指针。指针的值以依赖于实现的方式转换为可打印字符序列。

我猜,这意味着如果指针为 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).

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.

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.

jJeQQOZ5 2024-10-19 22:47:14

我假设 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.

懷念過去 2024-10-19 22:47:14

正如其他人所说,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.

薄情伤 2024-10-19 22:47:14

如果您的问题是将指针值与 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.

滴情不沾 2024-10-19 22:47:14

用 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 :-

变量的地址是---ps->p---删除后-- nil
变量的地址是---ps->p---删除后-- 0

对于 if 条件,它将计算为 false。

After casting the structure pointer with int , it provides the expected condition with if statement ..

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

it will evaluates to false for if condition .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文