How to append two arrays in C language?

Viewed 80618

How can I include the elements of array X and Y in to array total in C language ? can you please show with an example.

X = (float*) malloc(4);
Y = (float*) malloc(4);
total = (float*) malloc(8);

for (i = 0; i < 4; i++)
{
    h_x[i] = 1;
    h_y[i] = 2;
}

//How can I make 'total' have both the arrays x and y
//for example I would like the following to print out 
// 1, 1, 1, 1, 2, 2, 2, 2

for (i = 0; i < 8; i++)
    printf("%.1f, ", total[i]);
8 Answers

Here a solution to concatenate two or more statically-allocated arrays. Statically-allocated arrays are array whose length is defined at compile time. The sizeof operator returns the size (in bytes) of these arrays:

char Static[16];               // A statically allocated array
int n = sizeof(Static_array);  // n1 == 16

We can use the operator sizeof to build a set of macros that will concatenate two or more arrays, and possibly returns the total array length.

Our macros:

#include <string.h>

#define cat(z, a)          *((uint8_t *)memcpy(&(z), &(a), sizeof(a)) + sizeof(a))
#define cat1(z, a)         cat((z),(a))
#define cat2(z, a, b)      cat1(cat((z),(a)),b)
#define cat3(z, a, b...)   cat2(cat((z),(a)),b)
#define cat4(z, a, b...)   cat3(cat((z),(a)),b)
#define cat5(z, a, b...)   cat4(cat((z),(a)),b)
// ... add more as necessary
#define catn(n, z, a ...)  (&cat ## n((z), a) - (uint8_t *)&(z)) // Returns total length

An example of use:

char      One[1]   = { 0x11 };
char      Two[2]   = { 0x22, 0x22 };
char      Three[3] = { 0x33, 0x33, 0x33 };
char      Four[4]  = { 0x44, 0x44, 0x44, 0x44 };
char      All[10];
unsigned  nAll = catn(4, All, One, Two, Three, Four);

However, thanks to the way we defined our macros, we can concatenate any type of objects as long as sizeof returns their size. For instance:

char      One      = 0x11;                                // A byte
char      Two[2]   = { 0x22, 0x22 };                      // An array of two byte
char      Three[]  = "33";                                // A string ! 3rd byte = '\x00'
struct {
    char  a[2];
    short  b;
}         Four     = { .a = { 0x44, 0x44}, .b = 0x4444 }; // A structure
void *    Eight    = &One;                                // A 64-bit pointer
char      All[18];
unsigned  nAll     = catn(5, All, One, Two, Three, Four, Eight);

Using constant literals, one can also these macros to concatenate constants, results from functions, or even constant arrays:

// Here we concatenate a constant, a function result, and a constant array
cat2(All,(char){0x11},(unsigned){some_fct()},((uint8_t[4]){1,2,3,4}));

i like the answer from jon. In my case i had to use a static solution. So if you are forced to not use dynamic memory allocation:

int arr1[5] = {11,2,33,45,5};
int arr2[3] = {16,73,80};
int final_arr[8];

memcpy(final_arr, arr1, 5 * sizeof(int));
memcpy(&final_arr[5], arr2, 3 * sizeof(int));

for(int i=0; i<(sizeof(final_arr)/sizeof(final_arr[0]));i++){
    printf("final_arr: %i\n", final_arr[i]);
}

Why not use simple logic like this?

enter image description here

#include<stdio.h>  
  
#define N 5  
#define M (N * 2)  
  
int main()  
{  
    int a[N], b[N], c[M], i, index = 0;  
  
    printf("Enter %d integer numbers, for first array\n", N);  
    for(i = 0; i < N; i++)  
        scanf("%d", &a[i]);  
  
    printf("Enter %d integer numbers, for second array\n", N);  
    for(i = 0; i < N; i++)  
            scanf("%d", &b[i]);  
  
    printf("\nMerging a[%d] and b[%d] to form c[%d] ..\n", N, N, M);  
    for(i = 0; i < N; i++)  
        c[index++] = a[i];  
  
    for(i = 0; i < N; i++)  
        c[index++] = b[i];  
  
    printf("\nElements of c[%d] is ..\n", M);  
    for(i = 0; i < M; i++)  
        printf("%d\n", c[i]);  
  
    return 0;  
} 

Resultant array size must be equal to the size of array a and b.

Source: C Program To Concatenate Two Arrays

Related