As Ganado has said, the compiler has to know the array size up front (when defining an array.)
But you can leave the value out if there's an initializer list following the
declaration definition, e.g.
int temp[] = {1, 2, 3};
As you have not said otherwise, the compiler will take you mean
int temp[3] = {1, 2, 3};
i.e. provide just enough storage for the values you've provided, whereas if you'd written
int temp[8] = {1, 2, 3};
it would create an array of 8 elements with the first three elements set to 1, 2, and 3 with the rest set to zero.
The temp[] form is quite useful if you're too "lazy" to count the elements yourself as you can get the compiler to work this out for you, e.g.
1 2
|
const int pi_digits[] = {3, 1, 4, 1, 5, 9, 2, 6, 5};
const int pi_digit_count = sizeof(pi_digits)/sizeof(pi_digits[0]);
| |
If you decide to add more digits to Pi you shouldn't have to do more to your code than add the digits to the initializer list.
Andy
Edit: attempted to fix confusion between declaration and definition...