Arrays In C & C++

Page Contents

References

  1. How does this piece of code determine array size without using sizeof( )?, StackOverflow.com.
  2. Find size of array without using sizeof in C, StackOverflow.com.

Arrays And Pointers To Arrays

The reason this page exists because of this StackOverflow question. The OP supplies the following bit of code and asks how it can calculate the size of the array. It puzzled me (until I read the answers lol) so I'm jotting some notes down.

#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;
}

There was another post, pointed out, that supplied this code to do the same thing:

int main ()
{
    int arr[100];
    printf ("%d\n", (&arr)[1] - arr);
    return 0;
}