2 exp
2 exp
AIM: -
Write a C program to Copy one Array to another Arrays using Pointers
THEORY:
To copy one array to another in C, we use 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:
In the program, we have to copy one array to another array using pointers. For this
purpose, number of elements in the array 'n' and elements of the array sourceArray
are taken as an input from the user. Another array destinationArray is declared to
store the copy of sourceArray. Two pointers srcPtr and destPtr are declared and
points to array sourceArray and array destinationArray respectively.
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 .
4 . Display the Copied Array : 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]);
}
ALGORITHM:
1. START
2. INITIALISE sourceArray [MAX] AND destinationArray [MAX]
SIZE,I,* srcPtr,* destPtr.
3. INPUT AARAY SIZE AND ELEMENTS IN SOURCE ARRAY
4. SrcPtr AND destPtr points to base address of sourceArray[ ] and
destinationArray[ ];
srcPtr = sourceArray;
destPtr = destinationArray;
5. If value in sourceArray is TRUE
6. COPY Elements in Destination Array by incrementing address of SrcPtr AND
destPtr and copying
value at that address
* destPtr = * srcPtr
* srcPtr ++
* destPtr ++
Repeat STEP 5 till the value in source array
7. Print Copied Array as destinationArray[ ]
8. STOP
FLOWCHART:
CODING:
// 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);
OUTPUT:
CONCLUSION:
In this C program, we demonstrated how to copy the contents of one array into
another using pointers. By leveraging pointer arithmetic, we directly accessed the
memory locations of both arrays and efficiently transferred the data. This method
allows for more flexible and memory-efficient array manipulation, as pointers
provide a direct reference to memory addresses, eliminating the need for array
indexing.
The program works by iterating through the source array with a pointer and using
the pointer to copy each element into the destination array. This technique also
emphasizes the importance of understanding memory handling in C, particularly in
relation to pointers and arrays. Overall, this approach is both practical and
powerful for various scenarios where array data needs to be copied, providing a
deeper understanding of pointer usage in C programming.