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

    Expert

    Feb 24, 2006
    10,140
    1,789
    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
    802
    74
    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

    Senior Member

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

    AAC Fanatic!

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

    AAC Fanatic!

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

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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

    Moderator

    Mar 31, 2012
    17,737
    4,789
    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.
     
Loading...