Why does it omit the first array index in declaration?

Discussion in 'C Programming' started by fl, Jan 17, 2013.

  1. fl

    fl Guest

    Hi,

    I read the code of a sample application. Below is the main function part:
    ////////
    extern EDMA3_DRV_InstanceInitConfig sampleInstInitConfig[][EDMA3_MAX_REGIONS];

    void main()
    {
    Task_create((Task_FuncPtr)echo, NULL, NULL);

    BIOS_start();
    }
    ---------------
    In another .c file, it has:

    //////////////
    EDMA3_DRV_InstanceInitConfig sampleInstInitConfig[NUM_EDMA3_INSTANCES][EDMA3_MAX_REGIONS] =
    {
    /* EDMA3 INSTANCE# 0 */
    {
    .....
    }
    }
    -----------
    Of course, both NUM_EDMA3_INSTANCES and EDMA3_MAX_REGIONS are defined earlier. My question is about the declaration in main file. Why do they use [] in its first index:

    extern EDMA3_DRV_InstanceInitConfig sampleInstInitConfig[][EDMA3_MAX_REGIONS];

    Could you explain it to me?
    Thanks,
    fl, Jan 17, 2013
    #1
    1. Advertising

  2. fl

    Joe Pfeiffer Guest

    fl <> writes:

    > Hi,
    >
    > I read the code of a sample application. Below is the main function part:
    > ////////
    > extern EDMA3_DRV_InstanceInitConfig sampleInstInitConfig[][EDMA3_MAX_REGIONS];
    >
    > void main()
    > {
    > Task_create((Task_FuncPtr)echo, NULL, NULL);
    >
    > BIOS_start();
    > }
    > ---------------
    > In another .c file, it has:
    >
    > //////////////
    > EDMA3_DRV_InstanceInitConfig sampleInstInitConfig[NUM_EDMA3_INSTANCES][EDMA3_MAX_REGIONS] =
    > {
    > /* EDMA3 INSTANCE# 0 */
    > {
    > ....
    > }
    > }
    > -----------
    > Of course, both NUM_EDMA3_INSTANCES and EDMA3_MAX_REGIONS are defined earlier. My question is about the declaration in main file. Why do they use [] in its first index:
    >
    > extern EDMA3_DRV_InstanceInitConfig sampleInstInitConfig[][EDMA3_MAX_REGIONS];
    >
    > Could you explain it to me?
    > Thanks,


    Because it's not necessary there. What they're telling the compiler in
    main() is that this is an array, how many columns it has (which is
    necessary to figure out the offset to a given row), and that it's really
    being defined someplace else (that's what the extern means). Over in
    the other .c file where it's being defined, the number of rows is
    necessary so it can know how big the array is.
    Joe Pfeiffer, Jan 17, 2013
    #2
    1. Advertising

  3. fl

    Shao Miller Guest

    On 1/17/2013 11:31, fl wrote:
    > Hi,
    >
    > I read the code of a sample application. Below is the main function part:
    > ////////
    > extern EDMA3_DRV_InstanceInitConfig sampleInstInitConfig[][EDMA3_MAX_REGIONS];
    >
    > void main()
    > {
    > Task_create((Task_FuncPtr)echo, NULL, NULL);
    >
    > BIOS_start();
    > }
    > ---------------
    > In another .c file, it has:
    >
    > //////////////
    > EDMA3_DRV_InstanceInitConfig sampleInstInitConfig[NUM_EDMA3_INSTANCES][EDMA3_MAX_REGIONS] =
    > {
    > /* EDMA3 INSTANCE# 0 */
    > {
    > ....
    > }
    > }
    > -----------
    > Of course, both NUM_EDMA3_INSTANCES and EDMA3_MAX_REGIONS are defined earlier. My question is about the declaration in main file. Why do they use [] in its first index:
    >
    > extern EDMA3_DRV_InstanceInitConfig sampleInstInitConfig[][EDMA3_MAX_REGIONS];
    >
    > Could you explain it to me?
    > Thanks,
    >


    Mr. Joe Pfeiffer has already answered, but I also think it's worth
    noting that in the file with 'main', the 'sampleInstInitConfig' array
    has an incomplete object type, so you cannot iterate over its elements
    using:

    #define Countof(array) (sizeof (array) / sizeof *(array))

    extern void work_with(EDMA3_DRV_InstanceInitConfig * init_cfg);

    void some_func(void) {
    size_t i;

    /* Invalid use of 'sizeof' */
    for (i = 0; i < Countof(sampleInstInitConfig); ++i)
    work_with(sampleInstInitConfig);
    }

    --
    - Shao Miller
    --
    "Thank you for the kind words; those are the kind of words I like to hear.

    Cheerily," -- Richard Harter
    Shao Miller, Jan 17, 2013
    #3
  4. On Thu, 17 Jan 2013 10:58:53 -0700, Joe Pfeiffer
    <> wrote:

    > fl <> writes:
    >
    > > I read the code of a sample application. Below is the main function part:
    > > ////////
    > > extern EDMA3_DRV_InstanceInitConfig sampleInstInitConfig[][EDMA3_MAX_REGIONS];
    > >
    > > void main()


    #include std_disparagement_of_void_main /* but not relevant */

    > > In another .c file, it has:
    > > //////////////
    > > EDMA3_DRV_InstanceInitConfig sampleInstInitConfig[NUM_EDMA3_INSTANCES][EDMA3_MAX_REGIONS] =
    > > {
    > > /* EDMA3 INSTANCE# 0 */
    > > {
    > > ....
    > > }
    > > }
    > > -----------
    > > Of course, both NUM_EDMA3_INSTANCES and EDMA3_MAX_REGIONS are defined earlier.

    > My question is about the declaration in main file. Why do they use [] in its first index:


    (broken by hand because Agent is overly deferential to quoted lines;
    OP please try to put reasonable line breaks into google)

    > >
    > > extern EDMA3_DRV_InstanceInitConfig sampleInstInitConfig[][EDMA3_MAX_REGIONS];
    > >
    > > Could you explain it to me?
    > > Thanks,

    >
    > Because it's not necessary there. What they're telling the compiler in
    > main() is that this is an array, how many columns it has (which is
    > necessary to figure out the offset to a given row), and that it's really
    > being defined someplace else (that's what the extern means). Over in


    That's what extern *without an initializer* means. With an initializer
    extern is redundant (but may enhance clarity).

    > the other .c file where it's being defined, the number of rows is
    > necessary so it can know how big the array is.


    Yes but no. If the desired size of the array is equal to the number of
    elements provided in the initializer (at the leftmost dimension only
    for a multidim array as here) you can use empty brackets [] and the
    compiler determines the size from the initializer.

    But in some cases, especially if you need to have the count available
    to user code that doesn't/shouldn't see the definition, using a macro
    (or enum) for that count in both the clients and the definition can be
    helpful, and I would guess that's the OP's case.
    David Thompson, Feb 4, 2013
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kelvSYC
    Replies:
    6
    Views:
    7,226
    Richard Herring
    May 17, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,839
    Smokey Grindel
    Dec 2, 2006
  3. STF
    Replies:
    6
    Views:
    443
  4. Shawn W_
    Replies:
    5
    Views:
    273
    Aldric Giacomoni
    Sep 16, 2009
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    279
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page