Question about declaration of two dimension array

Discussion in 'C Programming' started by fl, Sep 15, 2013.

  1. fl

    fl Guest

    Hi,

    I read the following code snippet. I am not clear about the usage of 'sampleInstInitConfig[][EDMA3_MAX_REGIONS]'.

    When I replace the declaration with:
    sampleInstInitConfig[NUM_EDMA3_INSTANCES][EDMA3_MAX_REGIONS], it generates compiling error.

    In fact, EDMA3_MAX_REGIONS is defined external too.

    Could you explain the rule about the external, two dimension array?

    Thanks,



    --------------------
    main.c file:

    extern EDMA3_DRV_InstanceInitConfig sampleInstInitConfig[][EDMA3_MAX_REGIONS];

    int maio()
    {
    ....
    }

    --------------------
    Another .c file:

    EDMA3_RM_InstanceInitConfig sampleInstInitConfig[NUM_EDMA3_INSTANCES][EDMA3_MAX_REGIONS] =
    {
    /* EDMA3 INSTANCE# 0 */
    .....
     
    fl, Sep 15, 2013
    #1
    1. Advertisements

  2. fl

    Eric Sosman Guest

    For future reference: Telling people that "it generates compiling
    error" without mentioning *what* error is like telling your doctor
    "it hurts" without mentioning whether it's your elbow or your eyeball.
    Next time, copy and paste the actual error message.
    Do you mean NUM_EDMA3_INSTANCES?
    When you declare an array that is actually defined elsewhere
    you don't need to specify how many elements it has: A declaration
    like `extern Something list[];' is enough to go on, saying that
    some other module defines `list' as an array each of whose elements
    is a `Something'. You can make use of that array without stating
    how many `Something's there are -- the module that actually defines
    `list' must specify the count, but it isn't needed here.

    However, this ability to omit the element count only works
    for the leftmost dimension of a multi-dimensional array. That's
    because C's multi-dimensional arrays aren't quite like those of
    some other languages: They're actually one-dimensional arrays
    whose elements are themselves one-dimensional arrays. C needs
    to know the size of each array element in order to navigate from
    one to another, so if the elements are subordinate arrays C needs
    to know how many elements each has. All the remaining dimensions
    must be specified explicitly.

    The declaration that puzzles you means

    `sampleInstInitConfig' is an array of some unspecified
    number of elements. Each of those elements is itself
    an array of `EDMA3_MAX_REGIONS' elements, and each of
    *those* elements is an 'EDMA3_RM_InstanceInitConfig'.

    You *can* omit the leftmost array dimension -- but of
    course it's all right if you supply it (correctly), too. Why
    does the compiler "generates compiling error?"

    Because for an `extern' declaration (or any declaration at
    "file scope," outside any function), all the dimensions that
    you choose to specify must be given as compile-time constants.
    A variable is not a constant (not even if it has `const' in its
    declaration), so if you specify a variable name as a file-scope
    dimension the compiler will complain. (That's why I think you
    meant that NUM_EDMA3_INSTANCES rather than EDMA3_MAX_REGIONS is
    "defined external too," because if the latter were an `extern'
    variable then the compiler would complain about *both* versions.)
     
    Eric Sosman, Sep 15, 2013
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.