-1

Normally it's a question about a buffer with a null-terminated string, but we can extrapolate it to a general case.

I have a big array of a fixed length, let's say 10:

char outputArray[10] = {'-','-','-','-','-','-','-','-','-','-'};

And I have some other (Edited: smaller) array (in my case it's a char buffer with null terminator) with a variable length. Let's say it's a buffer of 6 elements, but the actual length is indicated by another variable.

char inputArray[10] = {'h','i','/0',...some other values, i'm not interested in};
int arrLength = 2; // For my task it means a strlen(inputArray);

How to put a small array at the end of a big array, to get this:

outputArray = {'-','-','-','-','-','-','-','-','h','i'} // the null terminator isn't important, it's not about the strings, it's about arrays.

Constraints:

  • I can't use std, so only "native" solutions (it's for Arduino)
  • C++11
  • Code should be memory and time efficient (some elegant algorithm without too much loops and too much temporary variables or calculations please)

Thank you in advance

Edited:

Thank to @ThomasWeller for an answer. I have a small precision though. What if I need to clean all the elements before the inserted array?

For example I had some garbage

{'a','k','$','-','n','"','4','i','*','%'};

And I need to get

{'-','-','-','-','-','-','-','-','h','i'};

Do I need 2 loops? First to reset an array and the second one to set the actual result?

4
  • "I have a big array of a fixed length, let's say 10" Hmm, interesting definition of "big"... (I'm not the downvoter.) Commented Sep 10, 2022 at 16:35
  • It sounds like memcpy might be your friend. Commented Sep 10, 2022 at 16:37
  • 1
    outputArray[8] = inputArray[0]; outputArray[9] = inputArray[1]; Commented Sep 10, 2022 at 16:38
  • memcpy(outputArray + sizeof outputArray - arrLength, inputArray, arrLength); Commented Sep 10, 2022 at 16:45

1 Answer 1

2

It can be done with a single for loop and a single variable:

  for (char i=0; i<arrLength; i++)
  {
    outputArray[10-arrLength+i] = inputArray[i];
  }

If you make arrLength a char instead of an int, this will even save you 2 bytes of memory ;-)

Use memset() to set all memory to an initial value and then memcpy() the contents at the end:

char output[10];
char input[10]  = "hi\0------";
char arrLength = 2;
void setup() {
  memset(output, '-', 10);  // "----------"
  memcpy(output+10-arrLength, input, arrLength);
  Serial.begin(9600);
  Serial.write(output, 10);
}
void loop() { }

Output in Arduino IDE

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for your response. I've edited my post to ask also how can I possibly reset all the values before the actual result? Thank you
@GZstudioGZstudio: you can use memset(). Adapted the answer
You're awesome dude! :) As I see, memset is more efficient than a loop, am I right?
@GZstudioGZstudio: I don't see a statement on the performance of memset. At least it should not be slower than a loop. Note that this might not apply, since Arduino does not have SSE or MMX instructions.
How important is performance and why?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.