E2021 Array must have at least one element (C++)

From RAD Studio
Jump to: navigation, search

Go Up to Compiler Errors And Warnings (C++) Index

ANSI C and C++ require that an array be defined to have at least one element (objects of zero size are not allowed).

An old programming trick declares an array element of a structure to have zero size, then allocates the space actually needed with malloc.

You can still use this trick, but you must declare the array element to have (at least) one element if you are compiling in strict ANSI mode.

Declarations (as opposed to definitions) of arrays of unknown size are still allowed.

Example

char ray[];         /* definition of unknown size -- ILLEGAL */
char ray[0];        /* definition of 0 size -- ILLEGAL */
extern char ray[];  /* declaration of unknown size -- OK */