Question, how to save a subportion of a char array into a new one?

Hello good people!

I keep using incorrect syntax for trying to get this to work, searched the arduino reference docs but didn't find anything specific.

Here's the problem:

I have a char array called Rec_char[10] containing 10 characters.

I want to extract the elements from 3 to 7 of the array and store it in a new one.
Is there a nice way of doing this?

Something like, NewArray[4] = Rec_char[3 untill 7]?

What is the right syntax for this?

What do you call « nice » ?

C does not have a syntax for extracting ranges in one go

You could go with a for loop

char destination[5];
for (int i=3; i<8;i++) {
  destination[i-3] = Rec_char[i];

Or copy the right part of the memory with memcpy()

char destination[5];
memcpy (destination, Rec_char+3, 5 ); // a char is 1 byte so 5 bytes to copy

Of course need to be careful not to overflow
Remember arrays start at index 0, so here you are copying entries# 4,5,6,7,8 which are at index 3,4,5,6,7

Just wondering, what's the type of NewArray?

Ther're both char arrays dougp, sorry for not making that clear.
Oh thank you J-M-L! That really does help! :slight_smile:
The reason i was asking if there was a "nice" way to do it was because of python, but we're in C.
Thanks for the help!

Remember as well that if you want to print the chars in the array in one go you need an extra entry in the array at the end with a NULL character (‘\0’) . That’s how cString mark the end of the text

Oh yeah I had to do that so i could print out the array and check if it has the values expected.
For that to work I had to append the array with a '\0' just as you said, thank you once again :smiley: