-3

at first I'll post the needed code.

#define STRING_LEFT        "05000858FB"
#define STRING_RIGHT       "05000B3E45"
#define STRING_LENGTH_MAX   16

char stringname[STRING_LENGTH_MAX+1];

if(strcmp(stringname, STRING_LEFT)==0)
{
// do stuff A here
}
else if(strcmp(stringname, STRING_RIGHT)==0)
{
// do stuff B here
}

stringname is triggered via a sensor that transfers a hexadecimal value and needs to be compared with STRING_LEFT and STRING_RIGHT followed by different actions. When stringname and STRING_LEFT are equal (==0) it does stuff A. If I activate STRING_RIGHT the next time, it does again stuff A. When I use RIGHT a second time, it does B. If I use LEFT again, it does B, a second time A. So the function is always like 'one-event-behind'....and I don't want to have that. How can I avoid this?

TIA

  • `stringname` is variable as well as function also in your code. where is `stringname` function? is any typo? – Jayesh Bhoi Jun 17 '14 at 09:21
  • Yeah, editet that, was just a typo here, not in the original code – user3743943 Jun 17 '14 at 09:35
  • 2
    Your description doesn't make sense. Possibly the contents of `stringname` actually are not what you think they are, but there's no way we can tell based on just what you have posted. Please post a sample program that doesn't rely on any external input (e.g. hardcode some sensor responses) and shows the problem. – M.M Jun 17 '14 at 09:37

2 Answers2

3

I think you have an important typo in your code:

else if(stringname(stringname, STRING_RIGHT)==0)

should probably be

else if(strcmp(stringname, STRING_RIGHT)==0)

(unless stringname is also a function besides a variable; in that case you should post that code, but that would be bad practice)

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • 1
    the original actually should not compile; even if there is a function `stringname` , one name will shadow the other (there's no "overloading" of names based on context in C). – M.M Jun 17 '14 at 09:35
1

I think you are trying to do following:

#include <string.h>
#include <stdio.h>

#define STRING_LEFT        "05000858FB"
#define STRING_RIGHT       "05000B3E45"
#define STRING_LENGTH_MAX   16

#define STRING_TO_COMPARE "05000858FB"

int main()
{
    char stringname[STRING_LENGTH_MAX+1];
    /* initialize the string to compare */
    strcpy(stringname, STRING_TO_COMPARE);

    if(strcmp(stringname, STRING_LEFT)==0)
    {
    // do stuff A here
    }
    /* call strcmp */
    else if(strcmp(stringname, STRING_RIGHT)==0)
    {
    // do stuff B here
    }
    return 0;
}

In that example, the stringname contains the same string than STRING_LEFT, so the program will execute stuff B. Of course you will have to set the stringname with the needed string.

daouzli
  • 15,288
  • 1
  • 18
  • 17