Pointers - 7 Example on Pointers & Arrays

Example on Pointers & Arrays :

# include < stdio.h >
main ( )
{
int *p;
int i, n, a [ 50 ] ;
printf ( " \n Enter the size of array " );
scanf ( "%d", &n );
p=a;    /* p = &a[0]; storing address of 1st element of array into   

                  pointer*/

printf ( " \n Enter the elements " );
for ( i = 0; i < n ; i++ )
{
  scanf ( "%d", *p );
  p++ ;  /* incrementing pointer to move to next memory location */
}
p = a; /* Repositioning pointer p to first element */
printf ( " \n Elements of array are \n " );
for ( i = 0; i < n; i++ )
{
  printf ( "%3d", *( p+i ) );
}
}



Comments