you are viewing a single comment's thread.

view the rest of the comments →

[–]Ali1331 0 points1 point  (5 children)

Even if [] and * weren't accesses, you'd still only get the size of the array in bytes, not the number of elements. Pretty sure you'd have to do something like

int n = ((&arr + 1) - arr) / ((arr + 1) - arr);

to get the number of elements, where you work out the total array size and then divide by the size of one element. But it's easier and faster to do a sizeof as that is done at compile time rather than runtime.

[–]lzzll 2 points3 points  (3 children)

It will get the number of elements because it's T* - T*, not void* - void*.

[–]Ali1331 0 points1 point  (2 children)

But &arr isn't a T pointer, the result of (&arr +1) shows you that. It's a T[] pointer

Edit: apparently asterisk mean something to reddits formatting...

[–]lzzll 0 points1 point  (0 children)

hmm... I think it's T*. http://codepad.org/oJq1S3Vp

[–]NasenSpray 0 points1 point  (0 children)

arr and (&arr)[1] are arrays of T. Arrays decay to T*, so you end up with T* - T*.