This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Array with Fully-Bracketed Initializers

Following on from this discussion:
http://www.8052.com/forum/read.phtml?id=84814


Keil C51 v7.50 rejects the following:

char key_tbl[][] =
{
    {   4,   3,   2,   1 },    // row 0
    {   8,   7,   6,   5 },    // row 1
    {  12,  11,  10,   9 },    // row 2
    {  16,  15,  14,  13 },    // row 3
};
It says:
MAIN.C(27): error C163: 'key_tbl': unknown array size
Since this is a Fully-Bracketed Initializer, shouldn't the compiler be able to deduce the size?

The description of this message in the online help doesn't mention this situation:

"In general, a formal size specifier is not required for external, single, or multi-dimensional arrays. Typically, the compiler calculates the size at initialization time. For external arrays, the size is of no great interest. This error is the result of attempting to use the sizeof operator on an undimensioned array or on a multi-dimensional array with undefined element sizes."

Parents
  • In a multi-dimensional array, only the leftmost [] can be left empty. All other bounds must be specified.

    Note that initializers are not all or nothing. You can legally initialize just a bit of an array, while all the rest default to zero:

    char arr[10] = { 'a', 'b', 'c' };

    This fact prevents the compiler from deducing the size of inner elements soley from the initializer.

    I've moved away from the practice of undeclared array sizes. Generally your specs or design docs tell you how many entries you must support. And the size of the array is often needed in loop bounds and other places. sizeof() can sometimes, but not always, determine the size of the array. So, I find it better to #define an array size constant (or often, use an extra value in a enum for that purpose). This constant is used in the array definition as well as other places to ensure consistency and allow the compiler to provide error messages for lack of initializers or too many initializers.

Reply
  • In a multi-dimensional array, only the leftmost [] can be left empty. All other bounds must be specified.

    Note that initializers are not all or nothing. You can legally initialize just a bit of an array, while all the rest default to zero:

    char arr[10] = { 'a', 'b', 'c' };

    This fact prevents the compiler from deducing the size of inner elements soley from the initializer.

    I've moved away from the practice of undeclared array sizes. Generally your specs or design docs tell you how many entries you must support. And the size of the array is often needed in loop bounds and other places. sizeof() can sometimes, but not always, determine the size of the array. So, I find it better to #define an array size constant (or often, use an extra value in a enum for that purpose). This constant is used in the array definition as well as other places to ensure consistency and allow the compiler to provide error messages for lack of initializers or too many initializers.

Children
No data