Searching for data in an array

Hello!

There is an array of data

char str[] = "Hello world#BEGtest#ENDHello world";

I need to get the content between #BEG and #END.

I wrote this example

void setup() {
 
  Serial.begin(115200);
  
  char str[] = "Hello world#BEGtest#ENDHello world";
  
  char *ptr_beg;
  char *ptr_end;

  ptr_beg = strstr (str, "#BEG");
  ptr_end = strstr (str, "#END");


  if (ptr_beg == NULL) {
    Serial.println("There is no beginning"); return;
    }


  if (ptr_end == NULL) { 
    Serial.println("There is no ending"); return;
    }

  ptr_beg+=4;

  for (ptr_beg; ptr_beg < ptr_end; ptr_beg++) {
  Serial.println(*ptr_beg);
  }

}

void loop() {

}

It works as it should.

How correct is this code?

Or does this task need to be solved in a different way?

Ok, that's good.

If it does what you want, then it's correct, no?

Again, if it does what you want, why would you "need" to solve it in a different way?

I sense either an XY problem or a homework assignment. Either way, there's a missing context here. Supply that and you'll have a better chance of getting an informed answer.

1 Like

You should not proceed if you did not find BEG, same for END and you might want to ensure that END is after BEG probably

So you could add some tests for that

This is not a homework assignment, but a fragment that I plan to use to parse data from an SD card. Am I using pointers correctly? :slightly_smiling_face:

See, that's a completely different question.

With @J-M-L 's comments in mind, yes.

But I still sense that you haven't stated what problem it is that you are encountering. If any.

So, XY problem.

        Serial.println("There is no beginning");
        return;

Where is the setup() function returning to ?

1 Like
  • Indentation is faulty
  • Arduino uses C++, so use nullptr instead of NULL
    • No need for the comparison anyway, since the pointer can be used in a boolean expression directly, with the ! operator
  • for initializer is pointless
    • Can use while instead of for since the post-increment can be done inline

More substantially, no point in scanning from the start for the #END; start after the #BEG (if you find that).

1 Like

If the source string modification is not prohibited, the above code could be simpler:

  *ptr_end = 0;
  Serial.println(ptr_beg);
  

I think this will print

test#ENDHello world
est#ENDHello world
st#ENDHello world
t#ENDHello world

Am I right?

Is that what you wanted?

Slightly different with the improvement that #END must be after #BEG

void setup() {
 
  Serial.begin(115200);
  
  char str[] = "Hello world#BEGtest#ENDHello world";
  
  char *ptr_beg;
  char *ptr_end;

  ptr_beg = strstr (str, "#BEG");
  if (ptr_beg == NULL)
  {
    Serial.println("There is no beginning"); 
    return;
  }

  ptr_beg += 4;
  ptr_end = strstr (ptr_beg, "#END");  //  forces that #END  will come after #BEG
  if (ptr_end == NULL)
  { 
    Serial.println("There is no ending"); 
    return;
  }

  while(ptr_beg < ptr_end) Serial.print(*ptr_beg++);

  Serial.println("\ndone");
}

void loop() {

}
1 Like

Hmm. Ok, so much for my theory. Perhaps someone can explain it for me.

EDIT: Maybe because your code is

Serial.println(*ptr_beg);

and not

Serial.println(ptr_beg);

I don't think so.
The *ptr_beg in the line

is a single char, not a pointer

2 Likes

Wow! Super! Thank you for sharing your experience! Now I can use this example to write the function I need for my project.

void setup() {
 
  Serial.begin(115200);
  
  char str[] = "Hello world#BEGtest#ENDHello world";
  
  char *ptr_beg;
  char *ptr_end;

  ptr_beg = strstr (str, "#BEG");
  if (!ptr_beg)
  {
    Serial.println("There is no beginning"); 
    return;
  }

  ptr_beg += 4;
  ptr_end = strstr (ptr_beg, "#END");  //  forces that #END  will come after #BEG
  if (!ptr_end)
  { 
    Serial.println("There is no ending"); 
    return;
  }

  while(ptr_beg < ptr_end) Serial.print(*ptr_beg++);

}

void loop() {

}

main() in that case.

using return; in a void C++ function is valid and not against the norm. It simply ends the function possibly early and is often used for clarity or to exit under certain conditions. Of course here it’s unnecessary and implicit.

1 Like

My question was rhetorical. I know where it returns to but I bet that @issaom78 doesn't, or what the possible effect of using it might be. As it happens it is benign

Perhaps more to the point is why you would use return in those circumstances in an Arduino sketch

Sorry - meant that more as clarification to your rhetorical question for future reader than as an answer for you as I know you know.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.