0
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?

sw30
  • 77
  • 5

5 Answers5

2

If an array size is not specified, the array length is determined from the initializer as follows:

  • If the array is of character type, the initializer may a string literal (prefixed as necessary for "wide" character types) - in that case, the size of the array is computed from the length of the literal, accounting for the string terminator (note that an empty string is *not* an empty initializer - it contains at least one element, the terminator itself)
  • Otherwise, the array is initialized with a brace-enclosed list of initializers and the array size is computed as follows:
    • If there is no designated initializer of the form [ constant-expression ] = initializer, then the size of the array is computed from the number of initializers - the declaration
      int a[] = {1, 2, 3};
      defines a to have 3 elements;
    • If there is one or more designated initializers, then the size of the array is computed from the largest of the designators *or* the number of initializers, whichever is larger - the declaration
      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.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • I don't understand the "*or* the number of initializers" sentence. Why does the number of initializers matter? Isn't the size of the array determined by the largest designator always? – cesss Oct 25 '21 at 20:08
  • 1
    @cesss: Imagine a declaration like `int a[] = { [0] = 9, 2, 3 };`. There are more initializers than the largest designator would indicate. – John Bode Oct 25 '21 at 20:22
  • That's it, John, I understand it now, thanks a lot! – cesss Oct 25 '21 at 21:30
1

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.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • "*syntax you gave with missing size and empty initializer list is **invalid***" Which C standard says so? – PM 77-1 Feb 11 '21 at 20:54
  • 3
    @PM77-1: C 2018 6.7.9 1 shows the grammar for initializers, and it shows an initializer must be a single value (an *assignment-expression*) or an *initializer-list* inside `{` and `}` and that an *initializer-list* contains one or more *initializer* items. – Eric Postpischil Feb 11 '21 at 20:59
  • Re “the size of the array is the number of initializers”: That is true for lists of simple initializers. Generally, the size of the array is just large enough to contain the explicitly initialized element with the largest index. E.g., `= { [55] = 3 }` makes the array size 56. – Eric Postpischil Feb 11 '21 at 21:01
  • 2
    @PM77-1 -- empty braces are not allowed in an initialization all the way back to C89 – ad absurdum Feb 11 '21 at 21:03
  • @adabsurdum - apparently were legalized in C99. – PM 77-1 Feb 11 '21 at 21:05
  • 3
    @PM77-1: What makes you think that? – Eric Postpischil Feb 11 '21 at 21:10
  • 3
    @PM77-1 -- standard C _never_ allowed empty braces initializers; IIRC C++ does allow them, and GCC seems to let you get away with them (but that is not documented). You should not rely on this, because it is not valid C. [See this.](https://stackoverflow.com/questions/17589533/is-an-empty-initializer-list-valid-c-code) – ad absurdum Feb 11 '21 at 21:14
0

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.

0

An array without any given size it's automatically generated accordin to number of parameters.

-1

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.

Kerby82
  • 4,934
  • 14
  • 48
  • 74