|
| 1 | +//The & operator is used to get the address. |
| 2 | +//But in case of array the name of array itself returns its address. |
| 3 | +//In array the elements occupy consecutive address, |
| 4 | +//therefore incrementing it by 1 each time would give |
| 5 | +//the address of next element. |
| 6 | + |
1 | 7 | #include<stdio.h> |
2 | 8 | int main() |
3 | 9 | { |
4 | 10 | int a[100],i,n,*add; |
5 | | - printf("enter the size"); |
| 11 | + |
| 12 | + printf("enter the size: "); |
6 | 13 | scanf("%d",&n); |
7 | | - printf("enter the no"); |
| 14 | + |
| 15 | + printf("enter the numbers: \n"); |
8 | 16 | for(i=0;i<n;i++) |
9 | 17 | { |
10 | 18 | scanf("%d",&a[i]); |
11 | 19 | } |
| 20 | + |
12 | 21 | for(i=0;i<n;i++) |
13 | 22 | { |
14 | | - add=(a+(i*sizeof(int))); |
15 | | - printf("%u\n",add); |
| 23 | + add=a+i; |
| 24 | + //add = &a[i]; would also return the same thing. |
| 25 | + printf("The address of element %d is %u.\n",*add, add); |
| 26 | + |
| 27 | + //Notice: As size of int is 4-byte the differnce in address |
| 28 | + //of cosecutive elements is 4. |
16 | 29 | } |
17 | 30 | } |
0 commit comments