Behavior of logical or

M

michaelp

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
 
C

Conor Hughes

michaelp said:
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.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

michaelp said:
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
 
M

michaelp

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.
'\0', of course. I expressed myself clumsily in the message. Sorry for that.
 
K

Keith Thompson

michaelp said:
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
}

What type is charVariable?

"\0" is not the same thing as '\0'.
 
M

michaelp

What type is charVariable?

"\0" is not the same thing as '\0'.

It is of course the latter I mean: '\0' . Hastily and clumsily written
message. Sorry for that
 
K

Keith Thompson

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

Then post a complete, compilable program that illustrates the problem.
We can't tell what's going on from the fragment you posted.

Keep in mind that null pointers, null charaters, and empty strings are
different things.
 
R

Rachael

Presumably the segfault is because you're trying to dereference
charVariable when it's NULL. Try charVariable instead of *charVariable.
 
L

Lew Pitcher

michaelp said:
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


)
{

[snip]

To summarize the discussion, your code probably should look like some
variant of

if ( charVariable != NULL)
{
if (*charVariable == '\0' ||
strncmp(charVariable,anotherCharVariable,2) == 0)
{
/* do something */
}
}
 
K

Kenneth Brody

michaelp said:
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?

You have already posted that you meant:

*charVariable == '\0'

rather than "\0".

However, your text says "charVariable is null", and given the usage
in the following strncmp, I assume you really mean "charVariable is
NULL". (I'm also assuming that charVariable is really a "char*" and
not a "char".)

In that case, you don't want the dereference. In other words, you
want to check is charVariable is a NULL pointer, rather than if it
points to a NUL ('\0') character.

if ( charVariable == NULL
||
strncmp(charVariable, anotherCharVariable, 2) == 0
)
...


--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top