本文介绍了逻辑或行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您好! 我在linux debian上运行gcc 2.95,并且遇到了一些奇怪的 行为。 我有这样一个表达式: if( * charVariable ==" \0" || strncmp(charVariable,anotherCharVariable,2)== 0 ) { //做某事 } 当charVariable为null时,我希望构造不要尝试评估下一个子表达式的 。在我看来它确实如此, 导致分段错误。 是否可以控制这种行为? Michael 解决方案 您是否尝试测试charVariable是否指向空字符''\0''或 如果它是空引用?你的代码暗示你正在尝试 前者,你的解释暗示你正在尝试后者。 这可能是错误的。" \0"评估为指针。比较字符串 和strcmp。 如果你想要chech,那么charVariable是一个NULL指针,你会这样做b $ b $ charVariable == NULL 如果你想检查它是一个空字符串,你会这样做 * charVariable ==''\ 0'' 或者charVariable [0] == 0 这就是我实际在做的事情。我在 消息中笨拙地表达了自己。 * charVariable ==" \0"当然是垃圾。 M. Hello! I am running gcc 2.95 on linux debian, and have come across some strangebehavior. 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 atevaluating the next sub-expression. It seems to me that it does,resulting in a segmentation fault. Is it possible to control this behavior? Michael 解决方案 Are you trying to test if charVariable points to the null character ''\0'' orif it is a null reference? Your code insinuates that you''re trying theformer, your explanation insinuates you''re trying the latter.This is likely wrong. "\0" evaluates to a pointer. Compare stringswith strcmp. If you want to chech wether charVariable is a NULL pointer you''ddocharVariable == NULLif you want to check wether it is an empty string, you''d do*charVariable == ''\0''or perhaps charVariable[0] == 0 This is what I am actually doing. I expressed myself clumsily in the message. *charVariable == "\0" is rubbish, of course. M. 这篇关于逻辑或行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-22 03:54