Collapsing two arrays into one in C and C++

A quick tip – if you have two arrays and you want to merge/collapse/concatenate/append/combine (whatever you want to call it) them into one here is one way to do it in C and C++. The code also prints the combined array to check it’s correct.

Note the subtle differences between C and C++. One of the (innumerable!) problems with C++ is that you can write as much C as you want in it and leave the next developer wondering why you were writing C instead of C++ and whether you had specific justifiable reasons to do so in each instance. I personally also consider it bad practice. This is one of the reasons I dislike hybrid languages – they allow you to blend two languages together in arbitrary proportions making the end result unreadable and unmaintainable and just raising more questions than the problems that it solves.

C

[c]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {

double a[4] = {1,2,3,4};
double b[4] = {5,6,7,8};

double* c = malloc(8 * sizeof(double));

memcpy(c, a, 4 * sizeof(double));
memcpy(c + 4, b, 4 * sizeof(double));

for (int i = 0; i < 8; i++) {
printf("%d %fn", i, c[i]);
}

free(c);

}
[/c]

C++

[cpp]
#include <iostream>
#include <cstring>
#include <iomanip>

using namespace std;

int main () {

double a[4] = {1,2,3,4};
double b[4] = {5,6,7,8};

double* c = new double[8];

copy(a, a + 4, c);
copy(b, b + 4, c + 4);

for (int i = 0; i < 8; i++) {
cout << i << " " << c[i] << endl;
}

delete[] c;

}
[/cpp]

Note that in the C++ program:

  • I’m not using includes from C
  • I’m not using memcpy from C
  • I’m not using printf from C
  • I’m not using malloc from C
  • I’m not using sizeof from C
  • I’m not using free from C
Advertisements

5 thoughts on “Collapsing two arrays into one in C and C++

  1. Bruce – the first argument to memcpy is not number of bytes but a pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*. c+4 is progressing the pointer to the fifth slot in the array. If you run the example the output should be correct.

  2. It seems that Dhruba always adopts and promotes best practices regardless of the endeavour (Java, C++ or even computer case layout…)

  3. Thanks Michael. I try. Although it is beginning to feel like an increasingly intellectually shallow and futile pursuit and concern, given the horrors I’ve witnessed in more recent times, I make every effort to consider about how others reading my code after me would interpret that code and what I can do to make that process easier. Doing so from the outset is always far easier and less costly than doing so in hindsight.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s