Help with basic C programming...

Please excuse the C newbie/very rusty programmer question.

I have the following defined in my code:

char* trigOptions[] = {"A", "M"};

I read a message string from the serial port into a string variable. I do some manipulation of the string and then pass it on to a processing function to pull out the fields and set my device variables. Inside a loop I want to compare a field value from the message to my definitions above to get the currently selected choice index. My code looks something like this:

int processSetupMessage(String message){

    int startIndex;
    int currentSelectionIndex;
    String value_str;
    char value_char[10];

    // Find the trigger setting
    startIndex = message.indexOf("TRIG#");
    value_str = message.substring(startIndex + 5, message.indexOf(":", startIndex));

   // Convert string to char[]
   value_str.toCharArray(value_char, 10);

   // Now compare for the right value
   for (int idx = 0; idx < 2; idx++){
      if (strcmp(trigOptions[idx], value_char) == 0){
         currentSelectionIndex = idx;
      }
   }

   return currentSelectionIndex;

I am trying to remember the code I ended with last night this morning. I think everyone can see where I am going with this. This does not work, though. Would appreciate any help.

Thanks again,
Rob Hix

Why are you using the String class when you are already using C string functions like strcmp?

Mainly because the Serial read function I am using (Serial.readStringUntil()) returns a string and the string is somewhat complex and I was taking the easy route so I could use indexOf() and other methods to parse the string easier.

Another reason is that I truly may not know any better methods. I would be open to better suggestions to implement at a later time. My deadline on this is approaching quickly and I need to get this working.

You haven't told us what doesn't work. It would help if you gave a more exact description of how it it supposed to function.

Yeah, that is what I get for trying to remember this when not sitting in front of it. I may have to wait till I can get back to it this afternoon and post a better description.

Basically, though, the strcmp() function never returns zero even though the value parsed from the message does exist in the option list. I am thinking I am messing up something with pointers that the compiler is perfectly fine with, however, it is not acting as I expect it to act.

I was mostly posting to see if anyone saw any glaring issues with the code.

robhix:
I think everyone can see where I am going with this.

Well, I hate to admit it, but I'm one of those dummies who doesn't see where you're going. The way to a good answer is a precisely-stated question and, to me, you haven't done that. Try this: what does the test input string look like? Does "TRIG#:A1234" even some close? Then, given the correct input string, what should the expected outcome look like? Without test data, which includes sample inputs and outputs, how can someone be expected to find a proper solution?

Also, drop the use of the String class. It's a crutch and eats more memory than standard C strings.

I will post a better stated an laid out question once I get back to the workbench. Like I said, I was trying to wing it to gain a few hours on the process.

I would love to use one method or the other (preferring the use of arrays for the reasons you stated) but it seems to be a real pain with complicated strings like this. I see now that the Serial library has a readBytesUtil() function that I could use to read into a char buffer.

Maybe it will come to me before I hit the bench later today. Id not, expect another post tonight. This time better thought out and more complete.

Sorry for wasting everyone's time.

why are you comparing strings if it’s only one char?

robhix:
Sorry for wasting everyone's time.

You're not wasting everyone's time...asking questions is part of the learning process. However, learning how to ask for help is also part of the learning process. As part of the process, try to learn what resources there are that already exist as part of the assault on your problem. For example, learn what C string functions exist and how to use them. One example is here.

If your only tool is a hammer, pretty soon every problem looks like a nail. Start hanging more tools from your belt by reading other coding examples and explain how each works to yourself. After a little practice, things will get a lot easier.