Char 指针与 NULL 的比较
我在 SUSE Linux 上运行我的代码。我有一个指针,我在函数中将其设置为= NULL。但是当我尝试在 while 循环中将同一指针与 NULL 进行比较时,问题就出现了。这导致程序崩溃。我在下面的示例代码中重现了我的问题。有人可以告诉我这里发生了什么事吗?
我的代码如下:
#include <stdio.h>
int func(char *,int);
int main()
{
char buffer[20];
int i =20;
int temp = func(buffer,i);
if ( (temp == 0) && (buffer != NULL) )
{
printf("inside loop \n");
}
}
int func(char *ad,int a)
{
ad = NULL;
printf("Integer is %d \n", a);
return(0);
}
问题是比较 buffer != NULL
失败,并且控制进入循环内部,这在理想情况下不应该发生。我已经解决了这个问题,方法是:
ad[0] = NULL
并将比较更改为buffer[0] != NULL
。
由于 NULL 仅在指针上下文中使用,因此这是错误的代码。我可以在解决方法中使用 '\0' 而不是 NULL,并且不用编写“糟糕的代码”,但我真的想知道这里发生了什么。有人可以澄清一下吗?
非常感谢, 阿迪亚
I am running my code on SUSE Linux. I have a pointer which I make = NULL in a function. But the problem arises when I try to compare the same pointer with NULL in a while loop. This is leading to the program crashing. I have reproduced my problem in the sample code below. Can someone please tell me what is happening here?
My code is as below:
#include <stdio.h>
int func(char *,int);
int main()
{
char buffer[20];
int i =20;
int temp = func(buffer,i);
if ( (temp == 0) && (buffer != NULL) )
{
printf("inside loop \n");
}
}
int func(char *ad,int a)
{
ad = NULL;
printf("Integer is %d \n", a);
return(0);
}
The problem is that the comparison, buffer != NULL
is failing and the control goes inside the loop, which should not be happening ideally. I have resolved this, by doing this:
ad[0] = NULL
and the comparison changed to buffer[0] != NULL
.
Since NULL is used only in a pointer context, this is bad code. I could use '\0's instead of the NULL in my workaround and get away with not writing 'bad code', but I really want to know what is happening here. Can someone please clarify?
Thanks a ton,
Aditya
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
buffer 不是指针,它不能为 NULL。
您的
func
将缓冲区的复制地址设置为NULL。这根本不影响缓冲区。编辑:扩展解释
这在堆栈上的某个位置保留了 20 个字节。它们没有以任何方式初始化。由于您有 20 个字节的内存,显然这些字节必须驻留在某个地址。
这将获取缓冲区中 20 个字节的地址,并将其传递给 func。
这里,在堆栈上的新位置处,有一个新的指针变量,该变量仅在
func
执行时存在。这个指针变量位于一个地址,并且指向一个地址。您更改此指针,这会影响它指向的位置。但它不会以任何方式更改您的原始缓冲区,因为ad
只是堆栈上的一个临时变量,其中包含buffer
变量中字节的地址(直到您设置这个临时变量为NULL)。buffer isn't a pointer, it cannot be NULL.
Your
func
sets the copied address of buffer to NULL. That does not affect buffer at all.EDIT: Extended explanation
This reserves 20 bytes somewhere on stack. They're not initialized in any way. As you have 20 bytes of memory, obviously these bytes must reside at some address.
This takes the address of your 20 bytes in buffer, and passes it to func.
Here you have, at a new position on stack, a new pointer variable which will exist only while
func
is executing. This pointer variable is at an address, and points to an address. You change this pointer, which affects where it points. But it will not change your original buffer in any way, sincead
is only a temporary variable on stack that contained the address of the bytes in yourbuffer
variable (until you set this temporary variable to NULL).两个问题:
在
func
中,您尝试修改ad
的值,而不是修改ad
的值> 指向。这是行不通的,因为参数值的更改不会反映在调用者中。您需要将该代码更改为<前><代码>
int func(char **ad, int a)
{
*广告=空;
printf("整数为 %d\n", a);
返回0;
}
不幸的是,这不适用于其余代码,因为...
buffer
被声明为数组对象,而不是指针,并且不能修改数组对象; IOW,buffer
无法分配。更不用说&buffer
的类型是char (*)[20]
,而不是char **
。这是您的源代码的修改版本,它将“起作用”,尽管我不确定您想要完成什么:
Two problems:
In
func
, you're attempting to modify the value ofad
, not whatad
points to. This won't work, as changes to the parameter value are not reflected in the caller. You would need to change that code toUnfortunately, this won't work with the rest of the code, since...
buffer
is declared as an array object, not a pointer, and you cannot modify an array object; IOW,buffer
cannot be assigned to. Not to mention that the type of&buffer
ischar (*)[20]
, notchar **
.Here's a modified version of your source that will "work", although I'm not sure what you're trying to accomplish: