Keil Logo Arm Logo

Discussion Forum

strncmp

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Details Message
Read-Only
Author
Eric Lewis
Posted
26-Oct-2000 23:52 GMT
Toolset
C51
New! strncmp
if (!strncmp((char *)(PillDBPtr - (DATA1 & 0x0F)-(DATA1 >> 4)), drec.bcdRXCheck, (DATA1 >> 4)))

Could anyone suggest why the above statement would evaluate to true on the following conditions.

PillDBPtr = 0x201F
DATA1 = 0x66
drec.bcdRXCheck => 0x00, 0x04, 0x50, 0x51, 0x36, 0x01

Data Locations starting at 0x2010:
00 43 28 00 00 00 00 00 00 30 04 51 36 10 50 66

What appears to be happening is that the equation evaluates the first byte,
0x2003 and bdrec.bcdRXCheck, and discontinues the evalution. Since the first bytes equate it evaluates to true. If I change this byte in either location if evaluates to false.

-Please help.
Read-Only
Author
Mark Odell
Posted
27-Oct-2000 02:04 GMT
Toolset
C51
New! RE: strncmp
This code is pretty obfuscated. If you re-write it so its intent is obvious, maybe you will be able to see the bug. I'm not to comfortable with your pointer arithmetic there. Is ANSI integer promotion disabled (you need to set this in the compiler settings)? In 10 years of using C51 I've never allowed ANSI integer promotion. If DATA1 is an SFR then you are actually reading its value 3 different times, possibly getting different values each time. Maybe take a snap shot of it with a normal variable.

- Mark
Read-Only
Author
Keil Support
Posted
27-Oct-2000 04:11 GMT
Toolset
C51
New! RE: strncmp
if (!strncmp((char *)(PillDBPtr - (DATA1 & 0x0F)-(DATA1 >> 4)), drec.bcdRXCheck, (DATA1 >> 4)))

Could anyone suggest why the above statement would evaluate to true on the following conditions.

PillDBPtr = 0x201F
DATA1 = 0x66
drec.bcdRXCheck => 0x00, 0x04, 0x50, 0x51, 0x36, 0x01

Data Locations starting at 0x2010:
00 43 28 00 00 00 00 00 00 30 04 51 36 10 50 66


The reason that strncmp STOPS when it sees a nul (00) is because a nul character represents the end of a string. The strncmp function compares at most the first n characters of each string. If the string is shorter than n characters, strncmp stops at the nul string terminator.

If you want to compare an exact number of characters, you should use the memcmp function.

Keil Support

Read-Only
Author
Mark Odell
Posted
27-Oct-2000 13:22 GMT
Toolset
C51
New! RE: strncmp
The reason that strncmp STOPS when it sees a nul (00)

My god, how did I miss this! I'm losing it.


- Mark
Read-Only
Author
Andrew Neil
Posted
4-Dec-2000 13:54 GMT
Toolset
C51
New! RE: strncmp
I think you've just illustrated your point about the obfuscated code: it was soooooooooo complicated, you assumed the error must be in there somewhere and missed the "obvious" answer!

(I hope that reads OK - it's supposed to be a compliment)

"Eschew obfuscation!"

Next Thread | Thread List | Previous Thread Start a Thread | Settings

arm-logo-small

Keil logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.