Reputation: 77
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?
Upvotes: 0
Views: 90
Reputation: 123458
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.
Upvotes: 2
Reputation:
An array without any given size it's automatically generated accordin to number of parameters.
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 5146
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.
Upvotes: -1
Reputation: 223872
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.
Upvotes: 1