How *(&arr + 1) - arr is working to give the array size [duplicate]

32
int arr[] = { 3, 5, 9, 2, 8, 10, 11 };      
int arrSize = *(&arr + 1) - arr;
std::cout << arrSize;

I am not able to get how this is working. So anyone can help me with this.

Share
Improve this question
11
  • 25
    This is not a usual idiom. Other readers of the code will be confused as well. Sure, it's a clever trick. Avoid cleverness like the plague. – dratenik May 12 at 19:14
  • 16
    Just use std::size – Ayxan Haqverdili May 12 at 19:15
  • 2
    @ThomasMatthews I was curious, but I dont think we can learn much from this: godbolt.org/z/YdWh8h8YY – largest_prime_is_463035818 May 12 at 19:30
  • 3
    This is the same question but for C: stackoverflow.com/q/56154380/1896169 The C and C++ standards are sufficiently different, however, that it makes sense to have separate questions – Justin May 13 at 4:12
  • 8
    FWIW, another alternative is sizeof(arr) / sizeof(arr[0]), IIRC. – Solomon Ucko May 13 at 17:42

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

flutter websocket connection issue