Learn about Read and write an array using function and pointer in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
C Program to Read and write an array using the pointer and function
Array elements can be accessed by using names and index. index 0 will point to base address of the array that is pointed using pointer. here, we can develop a program to access array elements using pointer referring to base address and function.
#include<stdio.h>
void read(int *p);
void display(int *q);
int main()
{
int a[5];
read( &a[0] );
display( &a[0] );
return 0;
}
void read(int *p)
{
int i;
printf("Enter array elements: ");
for(i=0;i<5;i++)
{
scanf("%d",p+i);
}
}
void display(int *q)
{
int i;
printf("Array elements are:\n");
for(i=0;i<5;i++)
{
printf("%d\t",*(q+i));
}
}
Output:
Enter array elements: 45
87
91
449
90
Array elements are:
45 87 91 449 90
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
C Program to Read and write an array using the pointer
C Program to swap Numbers in Cyclic Order using Pointer