Write a C program to copy one array elements to another array using pointers. How to copy array elements from one array to another array using pointers. Logic to copy one array to another array using pointers in C programming.
Example
Input
Input array1 elements: 10 -1 100 90 87 0 15 10 20 30
Output
Array1: 10 -1 100 90 87 0 15 10 20 30 Array2: 10 -1 100 90 87 0 15 10 20 30
Required knowledge
Basic C programming, Array, Pointers, Array and Pointers
Logic to copy one array to another array using pointers
Step by step descriptive logic to copy one array to another using pointers.
- Input size and elements in first array, store it in some variable say
size
andsource_array
. - Declare another array say
dest_array
to store copy ofsource_array
. - Declare a pointer to
source_array
say*source_ptr = source_array
and one more pointer todest_array
say*dest_ptr = dest_array
. - Copy elements from
source_ptr
todesc_ptr
using*desc_ptr = *source_ptr
. - Increment pointers
source_ptr
anddesc_ptr
by 1. - Repeat step 3 and 4 till
source_ptr
exists insource_arr
memory range.
Program to copy one array to another using pointers
/**
* C program to copy an array to another array using pointers
*/
#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size
/* Function declaration to print array */
void printArray(int arr[], int size);
int main()
{
int source_arr[MAX_SIZE], dest_arr[MAX_SIZE];
int size, i;
int *source_ptr = source_arr; // Pointer to source_arr
int *dest_ptr = dest_arr; // Pointer to dest_arr
int *end_ptr;
/*
* Input size and elements in source array
*/
printf("Enter size of array: ");
scanf("%d", &size);
printf("Enter elements in array: ");
for (i = 0; i < size; i++)
{
scanf("%d", (source_ptr + i));
}
// Pointer to last element of source_arr
end_ptr = &source_arr[size - 1];
/* Print source and destination array before copying */
printf("\nSource array before copying: ");
printArray(source_arr, size);
printf("\nDestination array before copying: ");
printArray(dest_arr, size);
/*
* Run loop till source_ptr exists in source_arr
* memory range.
*/
while(source_ptr <= end_ptr)
{
*dest_ptr = *source_ptr;
// Increment source_ptr and dest_ptr
source_ptr++;
dest_ptr++;
}
/* Print source and destination array after copying */
printf("\n\nSource array after copying: ");
printArray(source_arr, size);
printf("\nDestination array after copying: ");
printArray(dest_arr, size);
return 0;
}
/**
* Function to print array elements.
*
* @arr Integer array to print.
* @size Size of array.
*/
void printArray(int *arr, int size)
{
int i;
for (i = 0; i < size; i++)
{
printf("%d, ", *(arr + i));
}
}
Note: You can also write the above
while
loop aswhile(source_ptr <= end_ptr) *(dest_ptr++) = *(source_ptr++);
Output
Enter size of array: 10 Enter elements in array: 10 -1 100 90 87 0 15 10 20 30 Source array before copying: 10, -1, 100, 90, 87, 0, 15, 10, 20, 30, Destination array before copying: 0, 0, 127, 127, 0, 1, 0, 16777472, 0, 0, Source array after copying: 10, -1, 100, 90, 87, 0, 15, 10, 20, 30, Destination array after copying: 10, -1, 100, 90, 87, 0, 15, 10, 20, 0,