arr+1 vs &arr+1
int arr[5] = {10,20,30,40,50};
Here,
arr is an integer pointer (int*) which points the first element of the array.
&arr is an integer array pointer (int*)[5] which points the whole array. (all five elements.)
&arr + 1 | arr + 1 |
---|---|
&arr is a pointer to an entire array. So, if we move &arr by 1 position it will point the next block of 5 elements. |
arr is a pointer to the first element of the array.So, if we move arr by 1 position it will point the second element. |
If the array base address is 1000, &arr+1 will be 1000 + (5 * 4) which is 1020 |
If the array base address is 1000, arr+1 will be 1000 + 4 which is 1004 |
Example
#include<stdio.h> int main() { int arr[5] = {10, 20, 30, 40, 50}; printf("arr = %p \t arr+1 = %p\n",arr,arr+1); printf("&arr = %p \t &arr+1 = %p\n",&arr,&arr+1); return 0; }