Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Why does it omit the first array index in declaration? (http://www.velocityreviews.com/forums/t956613-why-does-it-omit-the-first-array-index-in-declaration.html)

fl 01-17-2013 04:31 PM

Why does it omit the first array index in declaration?
 
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,

Joe Pfeiffer 01-17-2013 05:58 PM

Re: Why does it omit the first array index in declaration?
 
fl <rxjwg98@gmail.com> 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.

Shao Miller 01-17-2013 09:40 PM

Re: Why does it omit the first array index in declaration?
 
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[i]);
}

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

Cheerily," -- Richard Harter

David Thompson 02-04-2013 08:08 AM

Re: Why does it omit the first array index in declaration?
 
On Thu, 17 Jan 2013 10:58:53 -0700, Joe Pfeiffer
<pfeiffer@cs.nmsu.edu> wrote:

> fl <rxjwg98@gmail.com> 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.


All times are GMT. The time now is 08:29 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57