int NULL的值 [英] Value of int NULL

查看:76
本文介绍了int NULL的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午所有,


我有一些代码(我认为可以,但现在感谢

可能不符合标准)包含


int a;

......

if(a == NULL)

{

....

}


与GCC一起编译很好 - 没问题,但PGI给了

错误。因此,我认为标准不允许检查整数是否为NULL

。但是,如果a被声明为这样,

没有值,那么

那个值是多少(或者是未确定的),有没有办法检查,不使用虚拟值,

整数变量是否分配了值?


我也注意到,虽然它产生了一个强制警告, PGI允许我

将NULL分配给一个整数,即int a = NULL;什么,或者它是具体实现的b $ b,是这个

案例中变量的实际值?


干杯,

尼克

Afternoon all,

I have some code (which I thought was OK, but now appreciate is
probably not by the standard) which contains

int a;
......
if (a==NULL)
{
....
}

Compiling with GCC was fine - no problem, however with PGI gave an
error. Therefore, I assume that checking that an integer contails NULL
is not allowed by the standard. But if a is declared like that,
without a value, then what is its value (or is it undetermined) at
that point, and is there a way to check, without using dummy values,
whether an integer variable has had a value assigned to it?

I also notice that, although it produces a cast warning, PGI allows me
to assign NULL to an integer i.e. int a=NULL; What, or is it
implementation specific, is the actual value of the variable in this
case?

Cheers,
Nick

推荐答案

2008-08-07,polas< ni ** @ helpforce.comwrote:
On 2008-08-07, polas <ni**@helpforce.comwrote:

下午全部,
Afternoon all,



嗨。

Hi.


我有一些代码(我认为可以,但现在感谢

可能不符合标准)其中包含


int a;

.....

if(a == NULL)

{

...

}


使用GCC进行编译很好 - 没问题,但PGI给出了

错误。因此,我认为标准不允许检查整数是否为NULL

。但是,如果a被声明为这样,

没有值,那么

那个值是多少(或者是未确定的),有没有办法检查,不使用虚拟值,

整数变量是否赋值给它?
I have some code (which I thought was OK, but now appreciate is
probably not by the standard) which contains

int a;
.....
if (a==NULL)
{
...
}

Compiling with GCC was fine - no problem, however with PGI gave an
error. Therefore, I assume that checking that an integer contails NULL
is not allowed by the standard. But if a is declared like that,
without a value, then what is its value (or is it undetermined) at
that point, and is there a way to check, without using dummy values,
whether an integer variable has had a value assigned to it?



未初始化变量的值未定义。如果你想检查

,请尝试打印你的整数。

没有办法检查是否有值赋值给变量。但是

你可以把它搞砸一些东西,然后检查它是否还是那样的。

例如。如果你希望将一个正数分配给你的整数,

你可以将它初始化为-1,然后检查是否为!= -1。

The value of an uninitialized variable is undefined. If you want to check
it out, try to print your integer.
There''s no way to check if ever a value was assigned to a variable. But
you can inizialize it to something, and later check if it''s still like that.
E.g. if you expect a positive number to be assigned to your integer,
you can initialize it to -1 and later check if a != -1.


我还注意到,尽管它产生了一个强制转换警告,PGI允许我将
分配给一个整数,即int a = NULL;什么,或者是具体实现的b $ b,是这个

案例中变量的实际值?
I also notice that, although it produces a cast warning, PGI allows me
to assign NULL to an integer i.e. int a=NULL; What, or is it
implementation specific, is the actual value of the variable in this
case?



NULL定义为0,这就是它的工作原理。虽然,你应该只用
将它用作指针,表示指针指向什么都没有。所以

编译器发出警告,以确保你正确使用NULL。


希望这会有所帮助。


干杯,

Salvatore。


-

| _ | 0 | _ | - Salvatore Iovene

| _ | _ | 0 | - 网站: http://www.iovene.com/

| 0 | 0 | 0 | - 密钥: http://www.iovene.com/pubkey.asc

------- - Webcomic: http:// geekhero .iovene.com /

NULL is defined to be 0, that''s why it works. Although, you should only
use it for pointers, to indicate that a pointer points to nothing. So the
compiler issues a warning so that you''d make sure you''re using NULL properly.

Hope this helps.

Cheers,
Salvatore.

--
|_|0|_| - Salvatore Iovene
|_|_|0| - Website: http://www.iovene.com/
|0|0|0| - Key: http://www.iovene.com/pubkey.asc
------- - Webcomic: http://geekhero.iovene.com/


polas< ni ** @ helpforce.comwrote:
polas <ni**@helpforce.comwrote:

下午所有,


我有一些代码(我认为可以,但现在感谢

可能不符合标准)包含


int a;

.....

if(a == NULL)

{< br $> b $ b ...

}


用GCC编译很好 - 没问题,但PGI给了

错误。因此,我认为标准不允许检查整数是否为NULL


Afternoon all,

I have some code (which I thought was OK, but now appreciate is
probably not by the standard) which contains

int a;
.....
if (a==NULL)
{
...
}

Compiling with GCC was fine - no problem, however with PGI gave an
error. Therefore, I assume that checking that an integer contails NULL
is not allowed by the standard.



整数不能包含空指针。


NULL是一个空指针常量。它_may_被定义为0(或

等价物),这意味着它看起来与空指针

常量完全相同,并且可以,令人困惑地用于整数上下文而不是指针上下文
。它也可以定义为((void *)0),或者

等价,在这种情况下它是一个指针,而不是一个整数,并且不能直接比较

一个整数。

在任何一种情况下,想想,NULL仍然是空指针常量,并且至少在概念上是一个指针,而不是一个整数,并且永远不应该是


用于表示整数值。

An integer cannot contain a null pointer.

NULL is a null pointer constant. It _may_ be defined as 0 (or
equivalent), which means that it will look exactly like the null pointer
constant, and can, confusingly, be used in an integer context rather
than a pointer context. It may also be defined as ((void *)0), or
equivalent, in which case it is a pointer, not an integer, and cannot be
directly compared to an integer.
In either case, thought, NULL is still the null pointer constant, and is
at least conceptually a pointer, not an integer, and should never be
used to represent integer values.


但是如果a被声明为没有值,那么它的值是什么

(或未确定)
But if a is declared like that, without a value, then what is its value
(or is it undetermined)



如果在功能范围内声明没有静态,则确实是

未定,甚至可能是陷阱表示。如果在文件范围内声明

,或者使用静态,则它始终为0 - 即零,_not_ a

空指针,即使NULL看起来如此喜欢0.

If it is declared at function scope without "static", it is indeed
undetermined, and may even be a trap representation. If it is declared
at file scope, or with "static", it is always 0 - that is, zero, _not_ a
null pointer, even where NULL looks like 0.


那个点,有没有办法检查,不使用虚拟值,

是否整数变量有一个分配给它的价值?
that point, and is there a way to check, without using dummy values,
whether an integer variable has had a value assigned to it?



编号即使在NULL看起来像0的情况下,将a与NULL进行比较也无法实现你想要的东西,因为0到0哪些静态对象初始化为
与你明确时得到的0完全相同




int a = 0 ;


Richard

No. And even where NULL looks like 0, comparing a to NULL does not do
what you want anyway, because the 0 to which static objects are
initialised is exactly the same as the 0 you get when you explicitly
write

int a=0;

Richard


polas写道:
polas wrote:

下午全部,


我有一些代码(我认为可以,但现在感谢

可能不符合标准)包含


int a;

.....

if(a == NULL)

{

...

}


与GCC一起编译很好 - 没问题,但PGI给了一个

错误。因此,我认为标准不允许检查整数是否为NULL


Afternoon all,

I have some code (which I thought was OK, but now appreciate is
probably not by the standard) which contains

int a;
.....
if (a==NULL)
{
...
}

Compiling with GCC was fine - no problem, however with PGI gave an
error. Therefore, I assume that checking that an integer contails NULL
is not allowed by the standard.



允许,但实现已定义。 NULL是一个将

扩展为空指针常量的宏。它可以是文字0或

表达式,如(void *)0或其他。


因为你无法确定它不是字面值0你不能安全

将它与整数值进行比较。它意味着*用于初始化

并与指针进行比较,而不是整数。对于整数,只需使用0


如果(a == 0)


更好更清晰。同样对于角色你可以使用


如果(c ==''\''')


虽然这里使用0而不是' '\'''非常便携,它是

neverthelss,不是那么可读。

It is allowed, but implementation defined. NULL is a macro that expands
to a null pointer constant. It could be either a literal 0 or an
expression like (void*)0 or something else.

Since you cannot be sure that it is not a literal 0 you cannot safely
compare it to integer values. It is *meant* to be used for initialising
and comparing with pointers, not integers. For integers just use 0

if (a == 0)

is better and clearer. Similarly for characters you can use

if (c == ''\0'')

although here using 0 instead of ''\0'' is perfectly portable, it is
neverthelss, not that readable.


但是如果a声明的那样,

没有值,那么

的价值是什么(或者是未确定的),有没有办法检查,而不使用虚拟值,

整数变量是否赋值给它?
But if a is declared like that,
without a value, then what is its value (or is it undetermined) at
that point, and is there a way to check, without using dummy values,
whether an integer variable has had a value assigned to it?



未初始化的对象具有不确定的值。为了完美地保证
安全,你甚至不应该检查这个值,因为它可能包含一个陷阱

表示。您只能在明确

或隐式初始化为某个值后使用对象。

An uninitialised object has an indeterminate value. To be perfectly
safe, you should not even inspect this value as it might contain a trap
representation. You can only use an object after it has been explicitly
or implicitly initialised to some value.


我也注意到,尽管它产生了一个强制警告,PGI允许我

为一个整数赋值NULL,即int a = NULL;什么,或者是具体实现的b $ b,是这个

案例中变量的实际值?
I also notice that, although it produces a cast warning, PGI allows me
to assign NULL to an integer i.e. int a=NULL; What, or is it
implementation specific, is the actual value of the variable in this
case?



这将因实施而异。它是
实现定义了什么值将写入指针,如果它是
指定为NULL。它也是实现定义了将指针转换为整数产生的值将是什么值。


在许多平台上它将为零,但是这不是由C

标准保证的。

This will vary from implementation to implementation. It''s
implementation defined what value will be written to a pointer if it is
assigned NULL. It is also implementation defined what value will be
produced as a result of converting a pointer to an integer.

On many platforms it will be zero, but this is not guaranteed by the C
Standard.


这篇关于int NULL的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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