I saw one approach to calculate the length of an array is like this:
int arr[5] = {5, 8, 1, 3, 6};
int len = *(&arr + 1) - arr;
I understand basic pointer arithmetic. For example arr + 1
move the pointer to the 2nd element in the array and the value of arr + 1
is the address of the 2nd element.
However I'm confused about what &arr + 1
does. I tried printing out the value of &arr
and it's the same as arr
, both are the address of the 1st element in the array. Any help is appreciated.