Board index » cppbuilder » strcmp not working????

strcmp not working????


2005-06-30 06:10:05 AM
cppbuilder31
#define GDF_HEADER "GDF"
unsined char ucHeader[3];
fread(ucHeader, sizeof(ucHeader), 1, fp);
Why does the first condition resolve to true but the second one to false?
if(ucHeader[0] == 'G' && ucHeader[1] == 'D' && ucHeader[2] == 'F')
{
// true
......
}
if(ucHeader == GDF_HEADER)
{
false
......
}
When I envoke the watch, both values of ucHeader & GDF_HEADER are identical
 
 

Re:strcmp not working????

"salamander1965" < XXXX@XXXXX.COM >wrote in message
Quote
if (strcmp(ucHeader, GDF_HEADER)==0)
Like you said, ucHeader is not null-terminated, so you should use strncmp()
instead (which you should use anyway since strcmp() is not safe in general):
if( strcmp(ucHeader, GDF_HEADER, 3) ==0 )
Gambit
 

Re:strcmp not working????

Quote
#define GDF_HEADER "GDF"
unsined char ucHeader[3];
fread(ucHeader, sizeof(ucHeader), 1, fp);

Why does the first condition resolve to true but the second one to false?

if(ucHeader[0] == 'G' && ucHeader[1] == 'D' && ucHeader[2] == 'F')
{
// true
......
}

if(ucHeader == GDF_HEADER)
{
false
......
}

When I envoke the watch, both values of ucHeader & GDF_HEADER are identical

First, your ucHeader is not zero-terminated so you can't compare it as a
string with "GDF". Second, by using ucHeader==GDF_HEADER, your actually
comparing a pointer to the ucHeader array with a pointer to the string
constant "GDF". You should use strcmp() to do the comparison in the
second if block:
if (strcmp(ucHeader, GDF_HEADER)==0)
{
...
}
But, you will need to have ucHeader zero-terminated:
#define GDF_HEADER "GDF"
unsigned char ucHeader[4];
fread(ucHeader, sizeof(ucHeader)-1, 1, fp);
ucHeader[3]=NULL;
etc...
If ucHeader is global, I think it will be initialized to zero, but I'm
not sure on this. Better to be safe and set ucHeader[3] yourself.
-- salamander
 

{smallsort}

Re:strcmp not working????

On Wed, 29 Jun 2005 18:10:05 -0400, "Stephane Baillargeon"
< XXXX@XXXXX.COM >wrote:
Quote
#define GDF_HEADER "GDF"
unsined char ucHeader[3];
fread(ucHeader, sizeof(ucHeader), 1, fp);
[snip]
if(ucHeader == GDF_HEADER)
{
false
......
}

When I envoke the watch, both values of ucHeader & GDF_HEADER are identical
Hi,
You are trying to compare pointers, not the strings.
Try to change
if(ucHeader == GDF_HEADER)
...
with
if ( !strcmp( ucHeader, GDF_HEADER ) )
...
Ciao
 

Re:strcmp not working????

Stephane Baillargeon wrote:
Quote
#define GDF_HEADER "GDF"
unsined char ucHeader[3];
fread(ucHeader, sizeof(ucHeader), 1, fp);

Why does the first condition resolve to true but the second one to false?

if(ucHeader[0] == 'G' && ucHeader[1] == 'D' && ucHeader[2] == 'F')
{
// true
......
}

if(ucHeader == GDF_HEADER)
{
false
......
}

When I envoke the watch, both values of ucHeader & GDF_HEADER are identical
When acessing a string of char's, the variable name is the address of the first element. So, you
are comparing two addresses which are no the same.
try :
if(strcmp(ucHeader, GDF_HEADER))
ShowMessage("They are different");
else
ShowMessage("They are the same");
Also be careful here:
fread(ucHeader, sizeof(ucHeader), 1, fp); // dangerous if it is a string, ok if not
fread(ucHeader, sizeof(ucHeader)-1, 1, fp); // safe
ucHeader[sizeof(ucHeader)-1] = 0; // terminate the string or it could get ugly
or
memset(ucHeader, 0, sizeof(ucHeader)); // fill it with NULL
fread(ucHeader, sizeof(ucHeader)-1, 1, fp); // Leave at least the NULL on the end
--
-Michael Gillen
 

Re:strcmp not working????

Quote
"salamander1965" < XXXX@XXXXX.COM >wrote in message
news:42c32265$ XXXX@XXXXX.COM ...


>if (strcmp(ucHeader, GDF_HEADER)==0)


Like you said, ucHeader is not null-terminated, so you should use strncmp()
instead (which you should use anyway since strcmp() is not safe in general):

if( strcmp(ucHeader, GDF_HEADER, 3) ==0 )

Your solution is clearly better than mine if for no other reason than
you don't have to manually put in a terminating null, and as you point
out, there are other reasons. I'm curious, though, about strcmp() not
being safe -- could you explain?
-- salamander
 

Re:strcmp not working????

"salamander1965" < XXXX@XXXXX.COM >wrote in message
Quote
I'm curious, though, about strcmp() not being safe -- could you explain?
My bad, strcpy() is the bad function to use. It is subject to buffer
overflowing, which is a common attack that hackers like to exploit.
Although, strcmp() is only not a very good function compared to strncmp(),
since strcmp() does not do any bounds checking, which is strcpy()'s weeknees
as well. If you have a buffer of N characters and compare it to a string of
M characters, where M>N, then the function can go outside the bounds of
the buffer and examine surrounding memory, which may or may not cause
problems.
Gambit
 

Re:strcmp not working????

Stephane Baillargeon wrote:
Quote
#define GDF_HEADER "GDF"
unsined char ucHeader[3];
fread(ucHeader, sizeof(ucHeader), 1, fp);

if(ucHeader == GDF_HEADER)
What everyone else said about why it doesn't work.....
They're right.
Just thought I'd drop in yet another way to compare the values, which
is faster, but I'm sure, frowned upon by the better-trained C/C++
programmers out there.
First, make the array 4 bytes instead of three
unsinged char ucHeader[4];
Then, after you have read 3 chars and set the 4th to zero.....
if( *(DWORD*)ucHeader == 'GDF' )
Notice that 'GDF' is not a string. It is a character constant.
No calls, a single hardware compare. Doesn't get much faster than
that.
 

Re:strcmp not working????

Remy Lebeau (TeamB) wrote:
Quote
Although, strcmp() is only not a very good function compared to strncmp(),
since strcmp() does not do any bounds checking, which is strcpy()'s weeknees
as well. If you have a buffer of N characters and compare it to a string of
M characters, where M>N, then the function can go outside the bounds of
the buffer and examine surrounding memory, which may or may not cause
problems.
If M and N are both zero-terminated, how can that be?
It is supposed to stop at the first null in either string.
The danger is if the logically shorter string isn't null terminated.
 

Re:strcmp not working????

Quote
If you have a buffer of N characters and compare it to a string of
M characters, where M>N, then the function can go outside the bounds of
the buffer ...
...unless the buffer of length N is zero-terminated, right? So the
problem with strcmp() comes about if one of the two char arrays being
compared is not a zero-terminated string (and also is the shorter of the
two).
I'm not trying to argue, just making sure I understand the problem. I
have some inline assembly that started off as a customized strcmp(), but
actually now more closely resembles strncmp() (although it's still
customized to my needs). After your comments, I want to go back through
it to make sure I have sufficient bounds checking.
-- salamander
 

Re:strcmp not working????

salamander1965 wrote:
Quote
...unless the buffer of length N is zero-terminated, right? So the
problem with strcmp() comes about if one of the two char arrays being
compared is not a zero-terminated string (and also is the shorter of the
two).
Right. Which IMHO isn't a problem at all. I don't think any of the
string functions that expect zero-terminated strings will work on a non
zero-terminated string. Is that a surprise to anyone? sin and cos
won't work on char*'s. Should they be avoided?
--
Gillmer J. Derge [TeamB]
 

Re:strcmp not working????

On Wed, 29 Jun 2005 18:10:05 -0400, Stephane Baillargeon wrote:
Quote
if(ucHeader == GDF_HEADER)
{
false
......
}
Of course that's false.... you're comparing two different pointers
and btw, ucHeader should be of length 4
--
Good luck,
liz
 

Re:strcmp not working????

Bob Gonder wrote:
Quote
unsinged char ucHeader[4];
Then, after you have read 3 chars and set the 4th to zero.....
if( *(DWORD*)ucHeader == 'GDF' )
Notice that 'GDF' is not a string. It is a character constant.
But three characters are implicitly converted to 4 bytes. The
statement
if( *(DWORD*)ucHeader == 'GDF\0' )
behaves the same. But now the position of the zero is
clearly seen.
I do not understand that the zero is implicitly added as fourth byte.
Why not like '\0GDF' ?
Hans.
 

Re:strcmp not working????

Gillmer J. Derge [TeamB] < XXXX@XXXXX.COM >wrote:
Quote
salamander1965 wrote:
>...unless the buffer of length N is zero-terminated, right? So the
>problem with strcmp() comes about if one of the two char arrays being
>compared is not a zero-terminated string (and also is the shorter of the
>two).

Right. Which IMHO isn't a problem at all. I don't think any of the
string functions that expect zero-terminated strings will work on a non
zero-terminated string. Is that a surprise to anyone? sin and cos
won't work on char*'s. Should they be avoided?
You have to try hard to manage to pass a 'char*'
to 'sin()'. It's relatively easy to pass a string
without a terminating '\0' to the C lib string
functions. Also, with passing garbage to 'sin()',
I think the worst that could happen is that you'll
get garbage results. With passing non-terminated
strings to the string functions, you might mess
things up badly.
Schobi
--
XXXX@XXXXX.COM is never read
I'm Schobi at suespammers dot org
"Coming back to where you started is not the same as never leaving"
Terry Pratchett
 

Re:strcmp not working????

Hans Galema wrote:
Quote
But three characters are implicitly converted to 4 bytes. The
statement

if( *(DWORD*)ucHeader == 'GDF\0' )

behaves the same. But now the position of the zero is
clearly seen.

I do not understand that the zero is implicitly added as fourth byte.
Why not like '\0GDF' ?
Because Intel is little endian?
Leading \0 would be multiplying the constant by 16.
'GDF' is 0x00464347
\0 F D G