C program to print array using pointer

Simple C program to print array using pointer: To print array element using pointer, first we declare a pointer to integer and then we initialize it to address of first element. Address of first element of array is given by the name of array. Suppose array is arr[3]={2,8,3} then address of element 2 is given by arr.
 
/* C program to print array using pointer */
#include<stdio.h>
int main()
{
 int n;
 printf("Enter size of array: ");
 scanf("%d",&n);
 int arr[n],*ptr,i;
 printf("Enter array elements: \n");
 for(i=0;i<n;i++)
 scanf("%d",&arr[i]);
 /* initialize address of first array element into pointer */
 ptr=arr;
 printf("Array: \n");
 for(i=0;i<n;i++)
 {
  printf("%d ",*(ptr+i));
 }
 return 0;
 
}
/* End of program */
 

Another C program to print array using pointer
 
/* C program to print array using pointer */
#include<stdio.h>
int main()
{
 int n;
 printf("Enter size of array: ");
 scanf("%d",&n);
 int arr[n],i;
 printf("Enter array elements: \n");
 for(i=0;i<n;i++)
 scanf("%d",&arr[i]);
 printf("Array: \n");
 for(i=0;i<n;i++)
 {
  printf("%d ",*(arr+i));
 }
 return 0;
 
}
/* End of program */
 




Popular posts from this blog