Translate

Labels

Saturday, 2 March 2013

ONE DIMENSIONAL ARRAYS USING POINTERS

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i;
clrscr();
printf("Enter no of elements\n");
scanf("%d",&n);
printf("Enter %d elements \n",n);
for(i=0;i<n;i++)
scanf("%d",a+i);
printf("The list if elements \n");
for(i=0;i<n;i++)
printf("%d \n",*(a+i));
getch();
}

In the above program, a is declared to be an array of int type of size 10.n and i are declared to be variables of int type.The variable n is to accept the number of elements to be accepted into array a, the value of which should lie within 1 and 10 and the variable i is to select each location of the array.We know that the array name a gives the address of the first location of the array and the expression a+i gives the address of the first location of the array and the expression a+i gives the address of i th location of the array.the following segment enables us to accept n values into the array.
for(i=0;i<n;i++)
scanf("%d",a+i)
The second argument a+i is equivalent to &a[i].*(a+0) gives the value at the first location of the array and in general,*(a+i) gives the value at i th location of array a.The following segment displays the n values stored in the array.
for(i=0;i<n;i++)
printf("%d \n",*(a+i));
the second argument *(a+i) is equivalent to a[i].

No comments: