• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

My own string compare does not work ?

Since i am more in the hardware than in the software, i still make stupid mistakes in programming. Since i am programming a small microcontroller and it has no string.h or stdio.h libraries, i had to come up with my own string compare.
Sadly, it always returns 1.
I am tired, i do not see it anymore. What have i done wrong ?

Code:
uint8_t StrCmp(uint8_t *strptr1, uint8_t *strptr2)
{
uint8_t		returnvalue;
uint8_t		safeguard;

	safeguard = 0;
	returnvalue = 1;
	while((*strptr1 != 0x00) && (*strptr2 != 0x00))
	{
		safeguard++;
		if(*strptr1 == *strptr2)
		{
			strptr1++;	
			strptr2++;	
		}
		else
		{
			returnvalue = 0;		
		}
	
		if(safeguard ==	255)
		{
			returnvalue = 0;
			break;
		}
	}
	return (returnvalue);
}
 
Nevermind. I found a better version in the internet.

Code:
int compare_string(char *first, char *second)
{
   while(*first==*second)
   {
      if ( *first == '\0' || *second == '\0' )
         break;
 
      first++;
      second++;
   }
   if( *first == '\0' && *second == '\0' )
      return 0;
   else
      return -1;
}
 
Standard string comparison returns 0 if they are the same, < 0 if the first is lower, and > 0 if the first is higher; your code is returning 0 if they're different. Anyway, in your code, you needed a break after the first returnvalue assignment.

Also, the standard return for a string comparison offers insight into how it's implemented: it's returning the difference between two characters.

E.g.,
Code:
int strcmp( const char *s1, const char *s2 )
{
	while (!(*s1 - *s2) && *s1)
	{
		++s1;
		++s2;
	}

	return(*s1 - *s2);
}

int strncmp( const char *s1, const char *s2, size_t bytes )
{
	while (!(*s1 - *s2) && *s1 && bytes--)
	{
		++s1;
		++s2;
	}

	return(*s1 - *s2);
}

strcmp (or strncmp, if you want to make sure it won't read past the buffer if there is no null terminator) is trivially simple, and the function that you found is needlessly complex. You need only a few lines of code. Note that you only need to check for the null termination on one of the strings, because if one string terminates before the other, it will be an inequality and the loop will stop, so you only need to check for the case where they terminate at the same time.
 
Last edited:
Thank you for your reply.

I will try your version instead.
I can add a check for null pointers as a protection.

These days i copy a lot more code than i used to.
Sometimes trying to reinvent the wheel is just not a good idea.

Now i still need to make an atoi, itoa, utoa and atou.
 
I decided to not do tests for null pointers.
My compiler generated errors on the "const char * " , i changed that to uint8_t . I use that for all characters anyway.
Thank you for your code example.

Code:
int16_t StrCmp( uint8_t *strptr1, uint8_t *strptr2 )
{
	while (!(*strptr1 - *strptr2) && *strptr1)
	{
		++strptr1;
		++strptr2;
	}

	return(*strptr1 - *strptr2);
}


Edit:
I see what your code does. It performs subtractions, comparison if equal is the same but your code should indeed produce smaller code.
 
Last edited:
My compiler generated errors on the "const char * " , i changed that to uint8_t . I use that for all characters anyway.
You should still specify const for both parameters since it is a hint to the compiler that they do not change, and so may help optiimisation.

Code:
int16_t StrCmp( const uint8_t *strptr1, const uint8_t *strptr2 )
 
Last edited:
Back
Top