Chathura Buddhika
Chathura Buddhika

Reputation: 2195

Copy pointer array values to end of another array in C

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.

Upvotes: 0

Views: 679

Answers (2)

Anonymous
Anonymous

Reputation: 2172

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]);

Upvotes: 0

regmagik
regmagik

Reputation: 844

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); 

Upvotes: 1

Related Questions