逻辑或行为 [英] Behavior of logical or

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

问题描述

您好!


我在linux debian上运行gcc 2.95,并且遇到了一些奇怪的

行为。


我有这样一个表达式:


if(

* charVariable ==" \0"

||

strncmp(charVariable,anotherCharVariable,2)== 0



{

//做某事

}


当charVariable为null时,我希望构造不要尝试评估下一个子表达式的
。在我看来它确实如此,

导致分段错误。


是否可以控制这种行为?


Michael

解决方案



" michaelp" < mi ****** @ hio.nowrote in message

news:eh ********** @ dolly.uninett.no ...
< blockquote class =post_quotes>
你好!


我在linux debian上运行gcc 2.95,并且遇到了一些奇怪的

行为。


我有这样一个表达式:


if(

* charVariable ==" \0" ;

||

strncmp(charVariable,anotherCharVariable,2)== 0




{

//做点什么

}


当charVariable为null时,我希望构造不要尝试

评估下一个子表达式。在我看来它确实如此,在分段错误中产生了



是否可以控制这种行为?



您是否尝试测试charVariable是否指向空字符''\0''或

如果它是空引用?你的代码暗示你正在尝试

前者,你的解释暗示你正在尝试后者。


michaelp写道:


你好!


我在linux debian上运行gcc 2.95,并且遇到了一些奇怪的

行为。


我有这样一个表达式:


if(

* charVariable ==" ; \0"



这可能是错误的。" \0"评估为指针。比较字符串

和strcmp。


如果你想要chech,那么charVariable是一个NULL指针,你会这样做b $ b $

charVariable == NULL

如果你想检查它是一个空字符串,你会这样做

* charVariable ==''\ 0''

或者charVariable [0] == 0


||

strncmp(charVariable,an otherCharVariable,2)== 0




{

//做点什么

}


当charVariable为null时,我希望构造不要尝试评估下一个子表达式的
。在我看来它确实如此,

导致分段错误。


是否可以控制这种行为?


Michael


On 25.10.2006 08:47,Nils O. Sel?sdal写道:


* charVariable ==''\'''



这就是我实际在做的事情。我在

消息中笨拙地表达了自己。 * charVariable ==" \0"当然是垃圾。


M.


Hello!

I am running gcc 2.95 on linux debian, and have come across some strange
behavior.

I have an expression like this:

if (
*charVariable == "\0"
||
strncmp(charVariable, anotherCharVariable, 2) == 0
)
{
//do something
}

When charVariable is null, I expect the construct not to attempt at
evaluating the next sub-expression. It seems to me that it does,
resulting in a segmentation fault.

Is it possible to control this behavior?

Michael

解决方案


"michaelp" <mi******@hio.nowrote in message
news:eh**********@dolly.uninett.no...

Hello!

I am running gcc 2.95 on linux debian, and have come across some strange
behavior.

I have an expression like this:

if (
*charVariable == "\0"
||
strncmp(charVariable, anotherCharVariable, 2) == 0
)
{
//do something
}

When charVariable is null, I expect the construct not to attempt at
evaluating the next sub-expression. It seems to me that it does, resulting
in a segmentation fault.

Is it possible to control this behavior?

Are you trying to test if charVariable points to the null character ''\0'' or
if it is a null reference? Your code insinuates that you''re trying the
former, your explanation insinuates you''re trying the latter.


michaelp wrote:

Hello!

I am running gcc 2.95 on linux debian, and have come across some strange
behavior.

I have an expression like this:

if (
*charVariable == "\0"

This is likely wrong. "\0" evaluates to a pointer. Compare strings
with strcmp.

If you want to chech wether charVariable is a NULL pointer you''d
do
charVariable == NULL
if you want to check wether it is an empty string, you''d do
*charVariable == ''\0''
or perhaps charVariable[0] == 0

||
strncmp(charVariable, anotherCharVariable, 2) == 0
)
{
//do something
}

When charVariable is null, I expect the construct not to attempt at
evaluating the next sub-expression. It seems to me that it does,
resulting in a segmentation fault.

Is it possible to control this behavior?

Michael


On 25.10.2006 08:47, Nils O. Sel?sdal wrote:

*charVariable == ''\0''

This is what I am actually doing. I expressed myself clumsily in the
message. *charVariable == "\0" is rubbish, of course.

M.


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

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