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
  • Out of "The C programming language" section A8.6.2:

    "Any type from which an array is constructed must be complete; it must not be an array or structure of incomplete type. This implies that for a multi-dimensional array, only the first dimension may be missing."

    Therefore it should be:

    char key_tbl[][4] =
    {
        {   4,   3,   2,   1 },    // row 0
        {   8,   7,   6,   5 },    // row 1
        {  12,  11,  10,   9 },    // row 2
        {  16,  15,  14,  13 },    // row 3
    };

    Frodak

Reply
  • Out of "The C programming language" section A8.6.2:

    "Any type from which an array is constructed must be complete; it must not be an array or structure of incomplete type. This implies that for a multi-dimensional array, only the first dimension may be missing."

    Therefore it should be:

    char key_tbl[][4] =
    {
        {   4,   3,   2,   1 },    // row 0
        {   8,   7,   6,   5 },    // row 1
        {  12,  11,  10,   9 },    // row 2
        {  16,  15,  14,  13 },    // row 3
    };

    Frodak

Children
  • "Out of 'The C programming language' section A8.6.2"

    Which edition of the book is that?
    Mine's the 2nd Edition, marked "Based on Draft-Proposed ANSI C," and contains that very paragraph.

    However, The ISO/IEC 9899:1990 standard doesn't - that's where all the stuff about fully-bracketed initialisers comes from
    (Section 6.5.7).

    There's a draft copy of the C9x spec here:
    http://members.lycos.co.uk/learnprogramming123/C9X.txt

    See section 6.7.8 and then the subsequent paragraph numbers #17 to #22 about array / structure initializers that are fully bracketed.

    However, as Drew says, it begs the question of how it would cope with something like:

    char key_tbl[][4] =
    {
        {   4,   3,   2,   1 },            // row 0
        {   8,   7,   6,   5 },            // row 1
        {  12,  11,  10,   9, 91, 92 },    // row 2!!
        {  16,  15,  14,  13 },            // row 3
    };
    

  • There's a draft copy of the C9x spec here:

    Careful there. That draft copy is a dangerous document to quote, these days. The document being drafted there is no longer in draft status, and there are non-negligible differences between the draft and the real thing.

    But that's not the worst of it. That document describes C99, but C51 doesn't even try to be C99 compliant (yet), so neither this draft nor the actual standard document that it eventually became, can meaningfully be applied to this problem.

    And I'm quite sure that C90 never allowed [][] arrays.

  • "That document describes C99, but C51 doesn't even try to be C99 compliant"

    I couldn't actually find a specific reference here or in the Manuals as to precisely what "ANSI" Keil C51 complies to?

    But, yes; I'm sure it's not C99.
    However, ISO/IEC 9899:1990 does contain nearly all of the words cited from that C99 draft (and differs from the book). The language is pretty opaque, but it does seem to be implying that it ought to work...?

    "And I'm quite sure that C90 never allowed [][] arrays."

    That's what I thought - and for the reasons already provided here.

  • <quote>
    "Out of 'The C programming language' section A8.6.2"

    Which edition of the book is that?
    Mine's the 2nd Edition, marked "Based on Draft-Proposed ANSI C," and contains that very paragraph.

    However, The ISO/IEC 9899:1990 standard doesn't - that's where all the stuff about fully-bracketed initialisers comes from
    (Section 6.5.7).
    </quote>

    My copy is Second Edition -- ANSI C.
    As others have mentioned I don't see where KEIL says which ANSI C standard they are trying to meet. I've always assumed ANSI-83 ( 83 is the 1st one isn't it? ).

    :-)
    Frodak

  • I've always assumed ANSI-83 ( 83 is the 1st one isn't it?

    No, it's C89 (ANSI) a.k.a. C90 (ISO edition of same). There was no earlier edition. Before ANSI C89, there was no formal standard definition of C, so the book written by the makers of C served that purpose.