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?
sizeof
. – Fenestrated%d
is certainly not strictly conforming and in fact would fail on a fairly normal-looking big-endian implementation with a 32 bitint
and a 64 bitptrdiff_t
. – Baptize%d
generates a warning on my compiler (clang). Should be%ld
– Brickbat