Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Wednesday, December 16, 2020

C program to copy one array to another using pointers

C program to copy one array to another | welocme2protec.com

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.



Here is the step by step logic to copy one array's elements to another array:

  1. First of all Create a function named display_arr to display array's elements.
  2. /*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));
       }
     }

  3. 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.
  4. int arr1[max_size], arr2[max_size];

  5. 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.

  6. /*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


  7. Input elements from user to arr1 by running a for loop.
  8. /*Running for loop to input array elements from user*/
     printf("Please input arr1 elements: ");
     for(i = 0; i <= size; i++){
        scanf("%d", ptr1 + i); 
     }

  9. Display elements of arr1 and arr2 by calling a created function display_arr(arr1, size)
  10. /*Displaying array elements of arr1 and arr2 before copying*/
    printf("\narr1 elements before coping: ");
    display_arr(arr1, size);

  11. The most important part of this program is that, copy elements of arr1 to arr2 like below.
  12. /*Copy elements of arr1 to arr2 by runing loop until arr1 exists in arr2*/ 
     while(ptr1 <= last_arr){
       *ptr2 = *ptr1;
        ptr1++;
        ptr2++;
     }

  13. Display again to chek elements of arr1 and arr2 by calling a created function display_arr(arr1, size)
  14. /*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






Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us