int array[] = {}
int array[length]
Why don't we need to put a length in the first syntax?
By filling out the {} does it set what values are already inside the array?
int array[] = {}
int array[length]
Why don't we need to put a length in the first syntax?
By filling out the {} does it set what values are already inside the array?
If an array size is not specified, the array length is determined from the initializer as follows:
[ constant-expression ] = initializer
, then the size of the array is computed from the number of initializers - the declarationint a[] = {1, 2, 3};
defines a
to have 3 elements;
int a[] = {[2] = 3};
defines a
to have 3 elements, and only initializes the third one (the first two are implicitly initialized to 0).
Arrays must have a non-zero size, and an empty initializer is not syntactically valid - at least one initializer must be present in the initializer list.
When an array has an initializer, the size may be omitted in which case the size of the array is the number of initializers. For example:
int array[] = { 1, 2, 3 };
The above array contains 3 elements because there are 3 elements in the initializer list.
The specific syntax you gave with missing size and empty initializer list is invalid as it would create an array with 0 elements.
in the first case you don't have to specify the size of the array because you are going to give the elements that you want inside the {}. So for example if you say : int array[] = {1,2,3,4}; this means that array's length is 4 and you fill the table with integers :1,2,3,4 . In the second case you can specify the size of matrix.So if you say for example int array[10] ; This means that you declare an array of type int and the maximum size of integers that you can read inside the matrix is 10.
An array without any given size it's automatically generated accordin to number of parameters.
The first line allocates an empty array so the length is zero, the second one allocates an array that can hold 5 elements.
It's important to note that the size and type of an array cannot be changed once it is declared.
So the empty array it's actually useless.