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 10 1304
"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.
On 25.10.2006 08:30, Conor Hughes wrote:
"michaelp" <mi******@hio.nowrote in message
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.
michaelp <mi******@hio.nowrites:
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'.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
On 25.10.2006 10:18, Keith Thompson wrote:
michaelp <mi******@hio.nowrites:
>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'.
It is of course the latter I mean: '\0' . Hastily and clumsily written
message. Sorry for that
>
michaelp <mi******@hio.nowrites:
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.
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.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Presumably the segfault is because you're trying to dereference
charVariable when it's NULL. Try charVariable instead of *charVariable.
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"
||
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 */
}
}
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"
||
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:Th*************@gmail.com> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ray Gardener |
last post by:
I've always wondered what other C++ programmers
fail to do while coding even though they know
what they're doing is... naughty (and some of the
items below occur in other languages, of course).
...
|
by: Mantorok Redgormor |
last post by:
#include <stdio.h>
struct foo { int example; struct bar *ptr; };
int main(void)
{
struct foo baz;
baz.ptr = NULL; /* Undefined behavior? */
return 0;
|
by: Bruno van Dooren |
last post by:
Hi All,
i have some (3) different weird pointer problems that have me stumped. i
suspect that the compiler behavior is correct because gcc shows the same
results.
...
|
by: [Yosi] |
last post by:
I would like to know how threads behavior in .NET .
When an application create 4 threads for example start all of them, the OS
task manager will execute all 4 thread in deterministic order manes,...
|
by: anyone.anon |
last post by:
Let p=malloc(N) for some N>0. As far as I understand it, free(p+k) for
0<k<N causes undefined behavior, since only a pointer returned by (m|
re|c)alloc() can validly be passed to free().
This...
|
by: Alan Isaac |
last post by:
Is there any discussion of having real booleans
in Python 3000? Say something along the line
of the numpy implementation for arrays of type 'bool'?
Hoping the bool type will be fixed will be...
|
by: iesvs |
last post by:
I got a double ** (double **coef).
And gcc accept the syntax coef, it works.
I don't understand why. For me coef is not correct because it's
a double ** and not type.
If someone could explain me...
|
by: Gabriel Genellina |
last post by:
En Sun, 07 Sep 2008 14:00:48 -0300, Patrick Maupin <pmaupin@gmail.comescribió:
Python takes some shortcuts when dealing with builtins. I'll just describe what happens (I won't say whether it is...
|
by: Stevo |
last post by:
If you split a string into an array using the split method, it's not
working the way I'd expect it to. That doesn't mean it's wrong of
course, but would anyone else agree it's working somewhat...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |