This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

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.

Parents
  • 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

Reply
  • 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

Children