i am new to c++ and stumbled upon this way of computing the length of an array with pointers which i don't exactly understand. I looked everywhere but nowhere seems to be an explanation on how it works, i just heard that it's supposed to be a bad way of computing array length but why is that and how does it even work?
The code would look something like this:
int array[4] = [0, 1, 2, 3]
//...
int length = *(&array + 1) - array
As far as i've tried, it really seems to work, but i don't exactly understand why. I know a bit of pointer logic but this statement seems really odd to me, because you're essentially taking the address of the array (the first element i suppose) and adding one to it (i can imagine that that will give you the address after the last element, but then i don't understand why you would dereference it). And what confuses me most, is that this all gets substracted with the array itself?! Without an index or anything.
It would really help when someone would be able to explain that to me, and why it's supposed to be bad exactly.
Thanks.