为变量(不是指针)分配NULL [英] Assigning NULL to a variable (not pointer)

查看:83
本文介绍了为变量(不是指针)分配NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得这是一个不好的做法,但找不到明确的

资源,说它很糟糕,为什么。任何人都可以帮忙吗?

解决方案

le * **** @ gmail.com 写道:


我记得这是一个不好的做法,但找不到确定的

资源说它很糟糕,为什么。有人可以帮忙吗?



< devilsadvocate>


如果除了你之外没有人认为这是一个不好的做法怎么办?并且不应该这是一个纯粹的事实,你无法找到任何明确的来源说明它是什么

坏的告诉你它可能不是那么糟糕?只是问......


< / devilsadvocate>


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


Leon发布:


我记得这是一个不好的做法,但无法找到明确的

资源这表明它很糟糕,为什么。有人可以帮忙吗?



想象两个不同的行星有两个完全不同的物种,两个

完全不同的文化。这两个行星不与每个行星进行交流,因为他们不知道对方是否存在。


给每个行星提供C ++标准和一个编译器。


一个星球将提出自己的理想和风格,而另一个星球将会产生不同的星球。


如果你的问题与C ++本身有关,那么答案就是这样:

对零使用NULL是无害的,你的编译器不会抱怨。


如果您的问题与目前存在于地球上的C ++文化有关,那么答案是:我们倾向于仅将NULL用于指针...和

对于整数使用NULL是非常不规则的。


所以如果你想适应,不要使用NULL对于整数。


如果你想对整数零使用NULL,那么继续,你的编译器

不会抱怨。


我自己从不使用NULL。


-


Frederick Gotham


le ***** @ gmail.com 写道:


我记得这是一个不好的做法,但找不到权威性的

资源,表明它是坏的,为什么。有人可以帮忙吗?



NULL计算为值为0的整数常量。如果要分配0,

使用0更清楚。如果您使用0而不是数值但是

作为一些错误/无错误标志,则以下将更具表现力:


const int MyFuncNoError = 0;


int myfunc()

{

// ...无论

返回MyFuncNoError;

}


if(myfunc()== MyFuncNoError)


使用NULL你只会混淆人们通过让它认为myfunc返回

某种类型的指针。


有些人对''\ 0''使用NULL,可能会混淆它NUL的名字是

ascii中的字符。这也引起了很多困惑:


if(somestr!= NULL&& * somestr!= NULL)


看着你可以认为somestr是char * *而不是char

*。


在一个句子中:NULL用于指针,如果你用它来b / b $ b其他东西你只会增加混乱并且没有任何好处。


-

Salu2


I remember that this is a bad practice, but can not find a definitive
resource that states it is bad and why. Can anyone help?

解决方案

le*****@gmail.com wrote:

I remember that this is a bad practice, but can not find a definitive
resource that states it is bad and why. Can anyone help?

<devilsadvocate>

What if nobody besides you think it''s a bad practice? And shouldn''t
the sheer fact that you can''t find any definitive source stating it''s
bad tell you that it might not be all that bad? Just asking...

</devilsadvocate>

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Leon posted:

I remember that this is a bad practice, but can not find a definitive
resource that states it is bad and why. Can anyone help?


Imagine two different planets with two totally different species, two
totally different cultures. These two planets don''t communicate with each
other as they are unaware of each other''s existance.

Give each of them the C++ Standard and a compiler.

One planet will come up with its own ideals and styles, and the other
planet will come up with different ones.

If your question is to do with C++ itself, then the answer is simply this:
Using NULL for zero is harmless, and your compiler won''t complain.

If your question is to do with the culture of C++ which exists presently on
Earth, then the answer is: We tend to use NULL only for pointers... and
it''s quite irregular to use NULL for integers.

So if you want to fit in, don''t use NULL for integers.

If you want to use NULL for integer zero, then go ahead, your compiler
won''t complain.

I myself never use NULL.

--

Frederick Gotham


le*****@gmail.com wrote:

I remember that this is a bad practice, but can not find a definitive
resource that states it is bad and why. Can anyone help?

NULL evaluates to a integer constant with value 0. If you want to assign 0,
using 0 is a lot more clear. And if you use 0 not by the numeric value but
as some error/no error flag, the following will be more expressive:

const int MyFuncNoError= 0;

int myfunc ()
{
// ...whatever
return MyFuncNoError;
}

if (myfunc () == MyFuncNoError)

Using NULL you just confuse people by making it to think that myfunc returns
some type of pointer.

Some people use NULL for ''\0'', probably confusing it with the NUL name of
the char in ascii. This also causes a lot of confusion:

if (somestr != NULL && * somestr != NULL)

Looking at this you can think that somestr is a char * * instead of a char
*.

In a sentence: NULL is intended to be used with pointers, if you use it to
other things you just add confusion and get no benefit.

--
Salu2


这篇关于为变量(不是指针)分配NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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