String Manipulation in C, AVR

Thread Starter

naqirizvi

Joined Jan 30, 2015
14
Hi,

I have to compare 2 strings.
I string is the tag I am getting from USART of 12 character.
other is the tag I declared using
Unsigned char tag1[12]="1234asdf5678";

I am new to programming.
if (tag==tag1){};isnot working
 

Papabravo

Joined Feb 24, 2006
21,157
There is no primitive operation in C that compares two strings. To do it yourself you have to write a loop or a recursive function to do it one character at a time. You could also use a library function if the compiler has one.

Also, when using the name of an array as you did the compiler converts it to &tag[0] and &tag1[0]. As such they can NEVER be equal. If you don't know what those notations mean or you don't understand why they can never be equal, then you need to dig deeper in your copy of K&R.
 

Shagas

Joined May 13, 2013
804
Arrays are stored as pointers in C. Your variable tag and tag1 are actually pointers to that array. When you do if(tag==tag1) you are comparing the pointers
of the arrays which are obviously not going to be the same here because they are pointing to different data.
 

ErnieM

Joined Apr 24, 2011
8,377
The strcmp function is a standard export of the string.h library, which should be art of your C package, as that is what makes C to the standard.

All you need do to use this function is #include <string.h>

It is a good thing to learn what comes for free in these libraries.
 
Last edited:

WBahn

Joined Mar 31, 2012
29,976
C# is miles away from C or C++, but surprisingly this function is the same.
Actually, the two functions would be very, very different since, in C#, a string is a true object. At best they would have kept the functional interface essentially the same, but in C# the string comparison function is string.Compare(s1, s2, case_insensitive) where s1 and s2 are string objects and case_insensitive is an optional Boolean expression.

I'm not aware of any strcmp() function in C#, but I could well be wrong.
 
Top