奇怪的整数指针行为 [英] strange integer-pointer behaviour

查看:64
本文介绍了奇怪的整数指针行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些狡猾的代码,乍一看,它是错误的,但它好像b $ b似乎正常工作

而且我没有任何编译器已经尝试过(目前已有五个,在各种系统上)

给出任何警告。

代码:

======= =====================

#include< stdio.h>


无效

fcn(char * str)

{

if(str ==''\''')

{

printf(" str!\ n");

}

}


int

main(无效)

{

fcn(''\ 0'');

返回0;

}

============================

到目前为止我的理解:''\'''传递给fcn(),它等于整数0,

反过来等于

a NULL指针就零度而言。然后将fcn()中的0与

比较为''\0'',这也是0.

没有警告,没有错误,代码有效(或者等等)好像)。

但是 - 如果我通过例如''\''到fcn()比所有编译器都抱怨

有类似的警告,

即从没有演员阵容的整数制作指针。


我的问题是,为什么编译器在''\0''通过时不会抱怨?

难道他们不应该给

同样的警告,因为它也是一个整数(虽然价值为0)?


-

WYCIWYG - 你的C是什么你得到了什么

I''ve ran into some fishy code that, at first glance, is buggy, but it
seems to work correctly
and none of the compilers I''ve tried (five so far, on various systems)
gives any warnings.
The code:
============================
#include <stdio.h>

void
fcn (char *str)
{
if (str == ''\0'')
{
printf ("str!\n");
}
}

int
main (void)
{
fcn (''\0'');
return 0;
}
============================
My understanding so far: ''\0'' is passed to fcn(), it equals integer 0,
which in turn "equals"
a NULL pointer in terms of zero-ness. The 0 in fcn() is then compared
to ''\0'' which is also a 0.
No warnings, no errors, code works (or so it seems).
But - if I pass e.g. ''\1'' to fcn() than all the compilers complain
with similar warnings,
i.e. making a pointer from integer without a cast.

My question is, why don''t the compilers complain when ''\0'' is passed?
Shouldn''t they give
the same warning, as it''s also an integer (albeit 0 in value)?

--
WYCIWYG - what you C is what you get

推荐答案

matevzb写于08/13/07 13:35,:
matevzb wrote On 08/13/07 13:35,:

我遇到了一些狡猾的代码,乍一看,它是错误的,但是它好像b $ b似乎正常工作

而且没有我试过的编译器(到目前为止,在各种系统上都是五个)

给出任何警告。

代码:

=== =========================

#include< stdio.h>


void

fcn(char * str)

{

if( str ==''\''')

{

printf(" str!\ n");

} < br $>
}


int

main(无效)

{

fcn(''\''');

返回0;

}

=========== =================

到目前为止我的理解:''\'''被传递给fcn(),它等于整数0,

反过来等于

a空指针的零指针。然后将fcn()中的0与

比较为''\0'',这也是0.

没有警告,没有错误,代码有效(或者等等)好像)。

但是 - 如果我通过例如''\''到fcn()比所有编译器都抱怨

有类似的警告,

即从没有演员阵容的整数制作指针。


我的问题是,为什么编译器在''\0''通过时不会抱怨?

难道他们不应该给

相同的警告,因为它也是一个整数(虽然值为0)?
I''ve ran into some fishy code that, at first glance, is buggy, but it
seems to work correctly
and none of the compilers I''ve tried (five so far, on various systems)
gives any warnings.
The code:
============================
#include <stdio.h>

void
fcn (char *str)
{
if (str == ''\0'')
{
printf ("str!\n");
}
}

int
main (void)
{
fcn (''\0'');
return 0;
}
============================
My understanding so far: ''\0'' is passed to fcn(), it equals integer 0,
which in turn "equals"
a NULL pointer in terms of zero-ness. The 0 in fcn() is then compared
to ''\0'' which is also a 0.
No warnings, no errors, code works (or so it seems).
But - if I pass e.g. ''\1'' to fcn() than all the compilers complain
with similar warnings,
i.e. making a pointer from integer without a cast.

My question is, why don''t the compilers complain when ''\0'' is passed?
Shouldn''t they give
the same warning, as it''s also an integer (albeit 0 in value)?



指针上下文中使用的文字常量零[*]

*是*空指针常量。在上面的代码中,''\'''

只是一种写文字常量零的装饰方式。

编译器发现它正在被使用其中指针是预期的
,因此编译器将零识别为null

指针常量而不是整数。实际上,你会发现在许多系统上,NULL宏被定义为
作为一个未经修饰的零。

[*]任何零值常量整数表达式将是b $ b;例如,尝试`fcn(42/100)''。此外,

表达式仍为空指针常量,如果它将
强制转换为void *。


这个特殊处理仅适用于


- 零值表达式:如果表达式'的值

非零,则表达式不能为空指针

不变。这就是为什么编译器在你试图使用''\'''作为fcn()的参数时抱怨



- 整数估价表达式:如果表达式''s $ type
是'double''或'float''或`struct fubar'',那就不是
a null指针常量。 fcn(0.0)不会工作。


- 常量表达式:如果表达式不是

编译时常量,则它不是null指针

常数。 fcn(x - x)不会工作;你可以推断

这个参数总是为零,但从编译器的角度来看,它只是一个非凡的

巧合。


常见问题解答有一节专门讨论空指针;你好

可能会觉得它很有用。


-
Er ********* @sun.com


8月13日晚上7:58,Eric Sosman< ; Eric.Sos ... @ sun.comwrote:
On Aug 13, 7:58 pm, Eric Sosman <Eric.Sos...@sun.comwrote:

指针上下文中使用的文字常量零[*]

*是*空指针常量。在上面的代码中,''\'''

只是一种写文字常量零的装饰方式。

编译器发现它正在被使用其中指针是预期的
,因此编译器将零识别为null

指针常量而不是整数。实际上,你会发现,在许多系统上,NULL宏被定义为
作为一个未经修饰的零。
A literal constant zero[*] used in a pointer context
*is* a null pointer constant. In the code above, ''\0''
is just a decorative way to write a literal constant zero.
The compiler sees that it''s being used where a pointer is
expected, so the compiler recognizes the zero as a null
pointer constant instead of as an integer. Indeed, you
will find that on many systems the NULL macro is defined
as an unadorned zero.



是的,抱歉甚至发布此信息。我发布后的片刻,我意识到

所有的赌注都是0关于(指针)类型

检查...

谢谢


-

WYCIWYG - 你得到的是什么

Yes, sorry about even posting this. Moments after I posted, I realized
that all bets are off with 0 with regards to (pointer) type
checking...
Thanks

--
WYCIWYG - what you C is what you get


matevzb写于08/13/07 14:30,:
matevzb wrote On 08/13/07 14:30,:

8月13日晚上7:58,Eric Sosman< Eric.Sos ... @ sun .comwrote:
On Aug 13, 7:58 pm, Eric Sosman <Eric.Sos...@sun.comwrote:

>指针上下文中使用的文字常量零[*]是*空指针常量。在上面的代码中,''\'''
只是一种编写文字常量零的装饰方式。
编译器看到它被用于指针的位置
期望的,因此编译器将零识别为null
指针常量而不是整数。实际上,您会发现在许多系统上,NULL宏被定义为一个未经修饰的零。
> A literal constant zero[*] used in a pointer context
*is* a null pointer constant. In the code above, ''\0''
is just a decorative way to write a literal constant zero.
The compiler sees that it''s being used where a pointer is
expected, so the compiler recognizes the zero as a null
pointer constant instead of as an integer. Indeed, you
will find that on many systems the NULL macro is defined
as an unadorned zero.



是的,抱歉甚至发布此信息。我发布后的片刻,我意识到

所有投注均为0关于(指针)类型

检查...


Yes, sorry about even posting this. Moments after I posted, I realized
that all bets are off with 0 with regards to (pointer) type
checking...



好​​吧,不,不完全是。没有必要键入 - 检查
一个NULL,因为NULL对于任何类型的指针来说都是一个非常好的值

指针。混淆(剩下的是什么)来自于有很多方法拼写NULL的事实。


Gertrude Stein从未说过A NULL is ''\ 0''是0,

但她可以。


-
呃********* @ sun.com


这篇关于奇怪的整数指针行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆