I was searching for a way to find the size of an array in C without using sizeof
and I found the following code:
int main ()
{
int arr[100];
printf ("%d\n", (&arr)[1] - arr);
return 0;
}
Can anyone please explain to me how is it working?
&a + 1 => It points at the address after the end of the array. *(a+1) => Dereferencing to *(&a + 1) gives the address after the end of the last element. *(a+1)-a => Subtract the pointer to the first element to get the length of the array. Print the size.
The idea is to use pointer arithmetic ( (&(var)+1) ) to determine the offset of the variable, and then subtract the original address of the variable, yielding its size. For example, if you have an int16_t i variable located at 0x0002 , you would be subtracting 0x0002 from 0x0006 , thereby obtaining 0x4 or 4 bytes.
We can find the size of an array using the sizeof() operator as shown: // Finds size of arr[] and stores in 'size' int size = sizeof(arr)/sizeof(arr[0]);
&arr
is a pointer to an array of 100 int
s.
The [1]
means "add the size of the thing that is pointed to", which is an array of 100 int
s.
So the difference between (&arr)[1]
and arr
is 100 int
s.
(Note that this trick will only work in places where sizeof
would have worked anyway.)
&arr
gives you a pointer to the array. (&arr)[1]
is equivalent to *(&arr + 1)
. &arr + 1
gives you a pointer to the array of 100 ints that follows arr
. Dereferencing it with *
gives you that array that follows. Since this array is used in an additive expression (-
), it decays to the pointer to its first element. The same happens to arr
in the expression. So you subtract to pointers, one pointing to the non-existent element right after arr
and the other pointing to the first element of arr
. This gives you 100.
But it's not working. %d
is used for int
. Pointer difference returns you ptrdiff_t
and not int
. You need to use %td
for ptrdiff_t
. If you lie to printf()
about the types of the parameters you're passing to it, you get well-deserved undefined behavior.
EDIT: (&arr)[1]
may cause undefined behavior. It's not entirely clear. See the comments below, if interested.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With