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?
issaom78:
It works as it should.
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
J-M-L
April 15, 2025, 5:01pm
3
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?
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
kenb4
April 15, 2025, 5:33pm
7
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
b707
April 15, 2025, 5:43pm
8
If the source string modification is not prohibited, the above code could be simpler:
*ptr_end = 0;
Serial.println(ptr_beg);
PaulRB
April 15, 2025, 6:10pm
9
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
PaulRB
April 15, 2025, 6:27pm
12
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);
b707
April 15, 2025, 6:40pm
13
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() {
}
J-M-L
April 16, 2025, 8:06am
15
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
J-M-L
April 16, 2025, 10:21am
17
Sorry - meant that more as clarification to your rhetorical question for future reader than as an answer for you as I know you know.
system
Closed
October 13, 2025, 10:21am
18
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.