Write a C program to copy one array elements to another array using pointers. Let's have a look at the below program how to store array elements by taking input form user and how to copy it to another array using pointers. See the below step by step logic to copy one array elements to another array.
Required knowledge-
C Basic programming | Array | pointer | Array and pointer | C function
Here is the step by step logic to copy one array's elements to another array:
- First of all Create a function named display_arr to display array's elements.
- Now Inside the main create two array named arr1 and arr2. It's size has already been defined to 50, like this #define max_size 50 at the starting of the program.
- Create two normal variables named size and i and three pointer variables *ptr1, *ptr2, & *last_arr.
👉 Where variable size will store size of the array and i will be used for running for loop.
- Input elements from user to arr1 by running a for loop.
- Display elements of arr1 and arr2 by calling a created function display_arr(arr1, size)
- The most important part of this program is that, copy elements of arr1 to arr2 like below.
- Display again to chek elements of arr1 and arr2 by calling a created function display_arr(arr1, size)
/*Creating a function to display array elements*/
void display_arr(int *arr, int size){
int i;
for(i = 0; i <= size; i++){
printf("%d ", *(arr + i));
}
}
int arr1[max_size], arr2[max_size];
/*Taking array size as an input from user and store it on variable size*/
printf("Enter size of array: ");
scanf("%d", &size);
/*Running for loop to input array elements from user*/
printf("Please input arr1 elements: ");
for(i = 0; i <= size; i++){
scanf("%d", ptr1 + i);
}
👉 And the pointer variables *ptr1, *ptr2, & *last_arr are pointing as follows:
int *ptr1 = arr1; //Pointing to arr1
int *ptr2 = arr2; //Pointing to arr2
int *last_arr = arr1+9; //pointing last element of arr1
/*Running for loop to input array elements from user*/
printf("Please input arr1 elements: ");
for(i = 0; i <= size; i++){
scanf("%d", ptr1 + i);
}
/*Displaying array elements of arr1 and arr2 before copying*/
printf("\narr1 elements before coping: ");
display_arr(arr1, size);
/*Copy elements of arr1 to arr2 by runing loop until arr1 exists in arr2*/
while(ptr1 <= last_arr){
*ptr2 = *ptr1;
ptr1++;
ptr2++;
}
/*Displaying array elements of arr1 and arr2 before copying*/
printf("\narr1 elements before coping: ");
display_arr(arr1, size);
C program to copy one array to another array using pointers
Now let's understand this program with a complete example, how to copy one array to another array and what is the output using pointers.
#include <stdio.h>
#define max_size 50
/*Creating a function to display array elements*/
void display_arr(int *arr, int size){
int i;
for(i = 0; i <= size; i++){
printf("%d ", *(arr + i));
}
}
int main(){
int arr1[max_size], arr2[max_size];
int size, i;
int *ptr1 = arr1; //Pointing to arr1
int *ptr2 = arr2; //Pointing to arr2
int *last_arr = arr1+9; //pointing last element of arr1
printf("Enter size of array: ");
scanf("%d", &size);
/*Running for loop to input array elements from user*/
printf("Please input arr1 elements: ");
for(i = 0; i <= size; i++){
scanf("%d", ptr1 + i);
}
/*Displaying array elements of arr1 and arr2 before copying*/
printf("\narr1 elements before coping: ");
display_arr(arr1, size);
printf("\narr2 elements before coping: ");
display_arr(arr2, size);
/*Copy elements of arr1 to arr2 by runing loop until arr1 exists in arr2*/
while(ptr1 <= last_arr){
*ptr2 = *ptr1;
ptr1++;
ptr2++;
}
/*Displaying array elements of arr1 and arr2 after copying*/
printf("\narr1 elements after coping: ");
display_arr(arr1, size);
printf("\narr2 elements after coping: ");
display_arr(arr2, size);
return 0;
Output:
Enter size of array: 8
Please input arr1 elements:
2 4 6 8 10 12 14 16
arr1 elements before coping:
2 4 6 8 10 12 14 16
arr2 elements before coping:
110 -10005555 4444 11111 100000 2 4 -16
arr1 elements after coping:
2 4 6 8 10 12 14 16
arr2 elements after coping:
2 4 6 8 10 12 14 16
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.