merging arrays

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can anybody help me with concatenation of arrays. I have two arrays.

char temp[4] = {0x41, 0x41, 0x55, 0x00};
and
char string1[] = "test";

and I want to join them together to get one array of:
{0x41, 0x41, 0x55, 0x00, 't','e','s','t'};

Does anybody have any suggestions as to how I would go about doing this? Thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well as long as they are C strings (that is ASCII characters with a final 0x00 terminator byte) the easy answer is strcat().

char destination [20];
strcpy(detination, temp);
strcat(detination, string1);
// now detination contains "AAUtest"

Otherwise, if it can be ANY data and you want to keep the 0x00's intact you probably want to use memcpy() but you'll need to know the length of the data:

char destination [20];
memcpy(detination, temp, 4);
memcpy(&detination[4], string1, 4);
// now detination contains 0x41, 0x41, 0x55, 0x00, 't','e','s','t'

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the help