How does this piece of code determine array size without using sizeof( )?
Going through some C interview questions, I've found a question stating "How to find the size of an array in C without using the sizeof operator?", with the following solution. It works, but I cannot understand why. #include <stdio.h> int main() { int a[] = {100, 200, 300, 400, 500}; int size = 0; size = *(&a + 1) - a; printf("%d\n", size); return 0; } As expected, it returns 5. edit: people pointed out this answer, but the syntax does differ a bit, i.e. the indexing method size = (&arr)[1] - arr; so I believe both questions are valid and have a slightly different approach to the problem. Than
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.