I am on a low level embedded system comparing a fixed size buffer of chars to a simple string. The buffer is much larger than the string so I think that that is why the comparison operators says that the two are not equal. This prompted me to write a simple function to compare the string to the beginning of the buffer – is this an elegant solution? Thanks
int CompareString(const char* string, int strlen)
{
//first check to see if the length is the same
//if there is a null char at string length then
//they are equal
if(MsgBuffer[strlen] != '\0')
{
return 0; //they are not equal
}
strlen = strlen - 1;
while(strlen > -1)
{
if(string[strlen] != MsgBuffer[strlen])
{
return 0; //they are equal
}
strlen = strlen - 1;
}
return 1; //they are equal
}
Here is some code you can use. Simply pass the comparison string and the buffer as two parameters. This code should also be fully compliant with MISRA-C:2004.