0

This is what I want. I tried it many ways, but not successful. I want to copy 2nd array values to end of 1st array.

uint8_t *set = getCharacterPattern('A');
uint8_t *set2 = getCharacterPattern('B');

// Here I want to copy *set2 values to end of *set array

for (uint8_t i=0; i<(getCharacterSize(A)+getCharacterSize('B')); i++){
    setLed(0,i,set[i]);
}    

Please help me someone.

1
  • I'm wondering why someone vote down my question... Commented Nov 30, 2014 at 18:06

2 Answers 2

1

You need to allocate memory for combined array and copy both arrays into the new memory. I assume getCharacterSize function returns the number of elements in corresponding array.

   // Combine arrays set and set2
   int sizeA = getCharacterSize('A');
   int sizeB = getCharacterSize('B');
   int sizeBoth = sizeA + sizeB;
   uint8_t *bothSets = malloc(sizeBoth * sizeof uint8_t);        
   memcpy(bothSets, set, sizeA * sizeof uint8_t);
   memcpy(bothSets+sizeA, set2, sizeB * sizeof uint8_t);

   // Use combined array
   for (uint8_t i=0; i<sizeBoth; i++){
     setLed(0, i, bothSets[i]);
   }

   // Release allocated memory
   free(bothSets); 
Sign up to request clarification or add additional context in comments.

4 Comments

The new and delete keywords are part of C++, not C.
Unless you are compiling with a C++ compiler, you do not want to cast the result of malloc() in a C program: stackoverflow.com/questions/605845/…
Sorry to be nitpicky, but C++ is not C and these details really matter.
@AlexReynolds, I agree, the details are important. Thanks for correcting me.
0
int len = getCharacterSize('A');
int len2 = getCharacterSize('B');
for (int i=0; i<len+len2; i++)
    setLed(0,i,i<len ? set[i] : set2[i-len]);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.