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?

4
  • 1
    {} denotes an empty array. And, yes, its size is already known. It's zero. Commented Feb 11, 2021 at 20:53
  • 4
    @PM77-1: C doesn't have zero-length arrays (or zero-length objects). Commented Feb 11, 2021 at 21:02
  • @rici - stackoverflow.com/a/14643530/2055998 Commented Feb 11, 2021 at 21:04
  • 5
    @PM77-1: That's a "flexible array member". It's not a zero-length array. Commented Feb 11, 2021 at 21:08

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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: Imagine a declaration like int a[] = { [0] = 9, 2, 3 };. There are more initializers than the largest designator would indicate.
That's it, John, I understand it now, thanks a lot!
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.

7 Comments

"syntax you gave with missing size and empty initializer list is invalid" Which C standard says so?
@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.
@PM77-1 -- empty braces are not allowed in an initialization all the way back to C89
@PM77-1: What makes you think that?
@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.
|
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.

Comments

0

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

Comments

-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.

1 Comment

so can the second one not hold 6? for example int array[6]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.