sw30
sw30

Reputation: 77

Questions about C array syntax

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

Answers (5)

John Bode
John Bode

Reputation: 123458

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.

Upvotes: 2

user5879787
user5879787

Reputation:

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

Upvotes: 0

KalkanisKir
KalkanisKir

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

Kerby82
Kerby82

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

dbush
dbush

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

Related Questions