C program to copy one array to another using pointers

Category: C Program
Tags: #cprogram#pointers#array

C program to copy one array to another using pointers. Learn how to copy arrays efficiently in C programming using pointers.

Copying elements from one array to another is a fundamental operation in programming, especially when dealing with arrays. In the C programming language, arrays and pointers go hand in hand, offering efficient ways to manipulate data. Utilizing pointers, we can achieve array copying efficiently and dynamically, allowing for more flexible and optimized code.

In this article, we'll explore how to copy one array to another using pointers in C. Understanding this concept is crucial for mastering array manipulation and memory management in C.

Understanding Pointers and Arrays in C

Before delving into array copying, let's grasp the basics of pointers and arrays in C.

  • Arrays: Arrays in C are collections of elements of the same data type stored in contiguous memory locations. They offer a convenient way to store and manipulate a fixed-size sequence of elements.
  • Pointers: Pointers are variables that store memory addresses. They allow direct access to the memory locations where data is stored. Pointers play a vital role in array manipulation, enabling efficient memory management and dynamic allocation.

Copying Arrays using Pointers

To copy one array to another in C, we leverage pointers to iterate through the elements of both arrays and perform the copy operation. Here's a step-by-step guide to copying arrays using pointers:

  1. Declare Arrays: Define two arrays - the source array (array from which elements will be copied) and the destination array (array to which elements will be copied).

    #define MAX_SIZE 100
    int sourceArray[MAX_SIZE];
    int destinationArray[MAX_SIZE];
    

    Then take input for array elements as we have discussed here.

  2. Declare Pointers: Declare pointers to the source and destination arrays.

    int *srcPtr = sourceArray;
    int *destPtr = destinationArray;
    
  3. Copy Elements using Pointers: Iterate through the source array using the source pointer and copy each element to the destination array using the destination pointer.

    for (int i = 0; i < SIZE; i++) {
        *(destPtr + i) = *(srcPtr + i);
    }
    
  4. Display the Copied Array (Optional): If required, display the elements of the destination array to verify the copy operation.

    printf("Copied Array: ");
    for (int i = 0; i < SIZE; i++) {
        printf("%d ", destinationArray[i]);
    }
    

C program to copy one array to another using pointers

Putting it all together, here's the complete C program to copy one array to another using pointers:

#include <stdio.h>

#define MAX_SIZE 100

int main() {
    int sourceArray[MAX_SIZE];
    int destinationArray[MAX_SIZE];
    int *srcPtr = sourceArray;
    int *destPtr = destinationArray;
    int size;

    printf("Enter the number of elements: ");
    scanf("%d", &size);

    printf("\nEnter %d array elements-\n", size);
    for (int i = 0; i < size; i++) {
        scanf("%d", &(*(sourceArray + i)));
    }

    // Copy elements using pointers
    for (int i = 0; i < size; i++) {
        *(destPtr + i) = *(srcPtr + i);
    }

    // Display the copied array
    printf("\nCopied Array-\n");
    for (int i = 0; i < size; i++) {
        printf("%d\n", destinationArray[i]);
    }

    return 0;
}

Output

Enter the number of elements: 5

Enter 5 array elements-
5
4
3
2
1

Copied Array-
5
4
3
2
1

Copying arrays using pointers is a fundamental operation in C programming. By leveraging pointers, we can efficiently manipulate array elements and manage memory dynamically.