8

How do I concatenate two arrays to get a single array containing the elements of both original arrays?

Jeff Atwood
  • 63,320
  • 48
  • 150
  • 153
osama
  • 51
  • 2
  • 2
  • 3
  • 1
    Yeah... Since both answers appear to be responding to the "concatenation" interpretation, and the OP hasn't returned to clarify, I've edited the question to reflect this. – Shog9 Nov 08 '09 at 20:48

1 Answers1

30

Arrays in C simply are a contiguous area of memory, with a pointer to their start*. So merging them involves:

  1. Find the length of the arrays A and B, (you will probably need to know the number of elements and the sizeof each element)
  2. Allocating (malloc) a new array C that is the size of A + B.
  3. Copy (memcpy) the memory from A to C,
  4. Copy the memory from B to C + the length of A (see 1).
  5. You might want also to de-allocate (free) the memory of A and B.

Note that this is an expensive operation, but this is the basic theory. If you are using a library that provides some abstraction, you might be better off. If A and B are more complicated then a simple array (e.g. sorted arrays), you will need to do smarter copying then steps 3 and 4 (see: how do i merge two arrays having different values into one array).


  • Although for the purpose of this question, the pointer explanation will suffice, strictly speaking (and for pacifying the commenter below): C has the concept of an array, that can be used without the syntax of pointers. Implementation wise, however, a C array and a contiguous area of memory, with a pointer are close enough they can be, and often are, used interchangeably.
Community
  • 1
  • 1
Chen Levy
  • 15,438
  • 17
  • 74
  • 92
  • Where did "...with a pointer to their start..." come from? When I declare `int a[10]` I get a contiguous area of memory with 10 `int` in it and no pointers whatsoever. – AnT stands with Russia Nov 08 '09 at 17:30
  • 2
    @AndreyT: `a` is your pointer. You appear to be aware of this in your comment on the other answer... – Shog9 Nov 08 '09 at 20:45
  • @Shog9: `a` is not a pointer. `a` is an array. When the array type decays to pointer type, the resultant pointer is just an intermediate temporary value, which has nothing to do with `a`. Wnat the post above states is incorrect, unless it talks specifically about `malloc`ed arrays. – AnT stands with Russia Nov 08 '09 at 20:53
  • A bit pedantic, eh? For the purpose of the algorithm described, it makes no difference (apart from the necessity of skipping step #5 in the case of stack-based arrays). – Shog9 Nov 08 '09 at 21:43
  • 2
    Stating that array is not a pointer is *not even remotely* pedantic. This is a rather popular and annoying misconception, so having it promoted here is definitely not a good idea. – AnT stands with Russia Nov 09 '09 at 06:18
  • @Shog9: int a[10] a in the symbol table has the memory address of the first element of a. this memory address call it whatever you want, is a memory address and not a C pointer. char *p, p in a symbol table has the address of a location to store p's value, and that value in a pointer to another value of type char. There is an extra dereference when using pointers and accessing them compared to arrays. – prime_number Nov 27 '09 at 07:51