简体   繁体   中英

How to merge two arrays in C using pointers

I know how to insert one array into pointer array. But I`m not able to merge arrays to a pointer array... Please can anyone help me to create a pointer array by merging two arrays....

#include<stdio.h>

int main(){

    int n,m,i,A[100],B[100], *C;

    printf("Enter size of A Array: \n");

    scanf("%d",&n);

    printf("Enter elements for A array: \n");

    for (i=0;i<n;i++)
    {
        scanf("%d",&A[i]);
        C++;
    }   
    C=&A[0];
    printf("\n Element in array are :\n");
    for(i=0;i<n;i++)
    {
        printf("\t%d",(*C));
        C++;
    }
    printf("Enter size of B Array: \n");

    scanf("%d",&m);

    printf("Enter elements for B array: \n");

    for (i=0;i<m;i++)

    {
        scanf("%d",&B[i]);
        C++;
    }   
    C=&B[0];
    printf("\n Element in array are :\n");
    for(i=0;i<m;i++)
    {
        printf("\t%d",(*C));
        C++;
    }
return 0;
}

If you want to merge two arrays ,you just need to copy the two array elements into a single array,you can write a simple code using pointers:

int main(){

int n,m,i,*A,*B,size=100;

A=malloc(size*sizeof(int));
B=malloc(size*sizeof(int));

printf("Enter size of A Array: \n");
scanf("%d",&n);

printf("Enter elements for A array: \n");
for (i=0;i<n;i++)
    scanf("%d",&A[i]);

printf("Enter size of B Array: \n");
scanf("%d",&m);

printf("Enter elements for B array: \n");
for (i=0;i<m;i++)
    scanf("%d",&B[i]);

for(i=n;i<n+m;i++){
   if(i>=size){
   size*=2;
   A=realloc(A,size*sizeof(int));
   A[i]=B[i];
   }
} 
printf("\n Element in array are :\n");
for(i=0;i<m+n;i++)
{
    printf("\t%d",(*A));
    C++;
}
return 0;
} 

i dont think the single pointer will be used in this case, with single pointer if you initialized the first array(take for an example 3 elements 1,2,3) and then you have to initialize it to the first location of the first array to print the sequence and then with the second array (3 elements 4,5,6) {after the first for loop :: C=&B[0]} so in the first for loop i hav incremented the C pointer and after that loop that means the next C location i have set the pointer to C=&B[0] and then do the same with the B array and set the pointer to the initial location and printed the values it gives the first tree values and some garbage values(the leftovers of the A array it didn't go to the B array address idk why) so i hav used the array of pointers

#include<stdio.h>
int main(){
int n,m,i,j,A[100],B[100], *C[100];
printf("Enter size of A Array: \n");
scanf("%d",&n);
printf("Enter elements for A array: \n");
for (i=0;i<n;i++)
{
    scanf("%d",(A+i));
    *(C+i)=A+i;

} 
printf("Enter size of B Array: \n");
scanf("%d",&m);
printf("Enter elements for B array: \n");
for (j=0;j<m;j++)
{
    scanf("%d",(B+j));
    *(C+i+j)=B+j;
}   
for (i=0;i<(n+m);i++){
printf("%d\t",*(C[i]));
}
return 0;
}

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