简体   繁体   中英

How to merge two arrays in C?

I was writing a program to concatenate two arrays in C. I am allocating memory for a third array and using memcpy to copy the bytes from the two arrays to the third. The test output is:

1 2 3 4 5 0 0 0 0 0

Is there anything wrong with this approach?

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int *array_concat(const void *a, int an,
                   const void *b, int bn)
{
  int *p = malloc(sizeof(int) * (an + bn));
  memcpy(p, a, an*sizeof(int));
  memcpy(p + an*sizeof(int), b, bn*sizeof(int));
  return p;
}

// testing
const int a[] = { 1, 2, 3, 4, 5 };
const int b[] = { 6, 7, 8, 9, 0 };

int main(void)
{
  unsigned int i;

  int *c = array_concat(a, 5, b, 5);

  for(i = 0; i < 10; i++)
    printf("%d\n", c[i]);

  free(c);
  return 0;
}
memcpy(p + an*sizeof(int),...

this second memcpy, you are trying to add 5 * sizeof(int) to an int pointer, p . However, when you add to a pointer, it already knows that it has to deal with sizeof(type) , so you don't have to tell it.

memcpy(p + an,...

Remove the multiplication *sizeof(int) from the 1st argument of memcpy. Keep it in the argument of malloc and the 3rd argument of memcpy.

This is because p + an points to an int which is an int s to the right from p -- that is, the int which is an*sizeof(int) bytes to the right from p .

p is a pointer to int. When you add an integer to a pointer to an int, the compiler multiplies the integer by the size of an integer. The net result is to multiply by the size of an integer twice : what you're getting is "p + an*sizeof(int)" is p + (number of elements in a) * (number of bytes in an int) * (number of bytes in an int).

memcpy(p + an*sizeof(int), b, bn*sizeof(int));

should be:

memcpy(p + an, b, bn*sizeof(int));

You should remove sizeof(int) from second memcpy where you use pointer arithmetic (+). Compiler doing this by itself depending on type of pointer.

you should see the definition of the memcpy, which copy's n "bytes" from the src to the dst area. so,you just need to times sizeof(int) only for the 3rd argument. and for "c", it's a pointer of int type, so, it does know that "+an" means move p forward to the an+1 int position.

Merging can be done by sorting the elements of the elements which are going to be merged code for merging two arrays

#include<stdio.h>
void sort(int arr[],int size){  // sorting function
    int i,j,temp;
    for(i=0;i<size;i++){
        for(j=i;j<size;j++){
            if(arr[i]>arr[j]){
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] =  temp;
            }
        }
    }
}
int main(){
    int a[10],b[10],c[10];
    int n,i,k=0,j=0;
    printf("Enter the size of the array:");
    scanf("%d",&n);
    for(i=0;i<n;i++){
        printf("Enter the element of array A at index %d:",i); //input array A
        scanf("%d",&a[i]);
    }
    sort(a,n);
    for(i=0;i<n;i++){
        printf("Enter the element of array B at index %d:",i);  //Input array B
        scanf("%d",&b[i]);
    }
    sort(b,n);
    for(i=0;i<(n+n);i++){   // merging the two arrays
        if(a[k]<b[j]){
            c[i] = a[k];
            k++;
        }
        else{
            c[i] = b[j];
            j++;
        }
    }
    printf("Merged Array :\n");
    for(i=0;i<(n+n);i++){
        printf("c -> %d ",c[i]);
    }
    return 0;
}

Reference C program to Merge Two Arrays after Sorting

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM