String Manipulation in C, AVR

Discussion in 'Embedded Systems and Microcontrollers' started by naqirizvi, Feb 2, 2015.

  1. naqirizvi

    Thread Starter New Member

    Jan 30, 2015

    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
  2. Papabravo


    Feb 24, 2006
    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.
  3. Shagas

    Active Member

    May 13, 2013
    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.
  4. nigelwright7557

    Distinguished Member

    May 10, 2008
    In C# you use the strcmp(string1,string2) function.
  5. kubeek

    AAC Fanatic!

    Sep 20, 2005
    C# is miles away from C or C++, but surprisingly this function is the same.
  6. shteii01

    AAC Fanatic!

    Feb 19, 2010
    First you have to store the incoming characters.
    Have you accomplished this?
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    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: Feb 3, 2015
  8. WBahn


    Mar 31, 2012
    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.