arraysclanguage-lawyer

Why can we use array of arrays in C?


Why can we use array of arrays in C? This was my question when I opened C standard language (C17).

I found something interesting (6.2.5 "Types" /20):

Any number of derived types can be constructed from the object and function types, as follows: — An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. The element type shall be complete whenever the array type is specified. Array types are characterized by their element type and by the number of elements in the array. An array type is said to be derived from its element type, and if its element type is T, the array type is sometimes called “array of T”. The construction of an array type from an element type is called “array type derivation”.

In C we have object types, function types, incomplete types. Elements of array have an object type, not derived type, and "what now?" thought I. And then I found this (6.7.6.2/1 and 6.7.6.2/3):

-In addition to optional type qualifiers and the keyword static, the [ and ] may delimit an expres sion or*. If they delimit an expression (which specifies the size of an array), the expression shall have an integer type. If the expression is a constant expression, it shall have a value greater than zero. The element type shall not be an incomplete or function type. The optional type qualifiers and the keyword static shall appear only in a declaration of a function parameter with an array type, and then only in the outermost array type derivation.

  • If, in the declaration “T D1”, D1 has one of the forms: D [ type-qualifier-listopt assignment-expressionopt ] D [ type-qualifier-listopt assignment-expression ] D [ type-qualifier-list static assignment-expression ] D [ type-qualifier-listopt ] and the type specified for ident in the declaration “T D” is “derived-declarator-type-list T”, then the type specified for ident is “derived-declarator-type-list array of T”.144) (See 6.7.6.3 for the meaning of the optional type qualifiers and the keyword static.)

In 6.7.6.2/1 I understood that type of element can't be function type and incomplete type, but then I understood that a type of element can be object type and derived type. In 6.7.6.2/3 if the type is derived we can create array of derived type. And in my opinion array - is derived type that can be classed to object type. But this only my opinion, I don't say that this is a truth. If I am not right, please explain why.


Solution

  • It looks like you got misled by this line:

    Any number of derived types can be constructed from the object and function types, as follows:

    which makes it sound like derived types are a separate category of type from object and function types.

    They're not. All types are either object types or function types. Being a derived type doesn't stop a type from being an object or function type.

    All array types are object types, so it's perfectly fine to have an array type whose element type is another array type.