Since &x[i] and (x+i) both represent the address of the ith element of x, it would seem reasonable that x[i] and *(x+i) both represent the content of the address i.e., the value of the ith element of x.
Here is a simple C program that illustrates the relationship between array elements and their addresses.
#include < stdio.h >
main()
{
static int x[10] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
int i;
for (i = 0;i <= 9; ++i) {
/*display an array element*/
printf("\ni= %d x[i]= %d *(x+i)= %d", i, x[i], *(x+i));
/*display the corresponding array address*/
printf(" &x[i]= %X x+i= %X", &x[i], (x+i));
}
}
Execution of the program results in the following output.
i=0 x[i]=10 *(x+i)=10 &x[i]=72 x+i =72
i=1 x[i]=11 *(x+i)=10 &x[i]=74 x+i =74
i=2 x[i]=12 *(x+i)=10 &x[i]=76 x+i =76
i=3 x[i]=13 *(x+i)=10 &x[i]=78 x+i =78
i=4 x[i]=14 *(x+i)=10 &x[i]=7A x+i =7A
i=5 x[i]=15 *(x+i)=10 &x[i]=7C x+i =7C
i=6 x[i]=16 *(x+i)=10 &x[i]=7E x+i =7E
i=7 x[i]=17 *(x+i)=10 &x[i]=80 x+i =80
i=8 x[i]=18 *(x+i)=10 &x[i]=82 x+i =82
i=9 x[i]=19 *(x+i)=10 &x[i]=84 x+i =84
No comments:
Post a Comment