INDEXING POINTERS
When an array is declared, the compiler
allocates a base address and sufficient amount of storage space for storing all
the elements of the array in successive memory locations. The name of the array is the beginning
address (first element index 0) of the array, called the base address. So, that the
array name is referred to as an address constant.
Example: int x[5] = {10,20,30,40,50};
Here,
x = &x[0] = &x all are referred to the address location 1000.
With the relationship between
the array and pointer, array subscripts can also be defined in terms of pointer
arithmetic operations which are known as indexing pointers.
For single dimensional
arrays,
int x[3], i=0;
&x[0] = x+0 x[0]
= *(x+0)
i.e., &x[i] = x+i x[i]
= *(x+i)
Similarly, for double
dimensional arrays,
&x[i][j] = (*(x+i)+j) x[i][j]
= *(*(x+i)+j)
#include<stdio.h>
#include<conio.h>
main()
{
int i,a[5]
= {10,20,30,40,50};
clrscr();
for(i=0;i<5;i++)
printf("a[%d]
: %d",i,*(a+i));
getch();
}
No comments:
Post a Comment