Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why does it omit the first array index in declaration?

Reply
Thread Tools

Why does it omit the first array index in declaration?

 
 
fl
Guest
Posts: n/a
 
      01-17-2013
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,
 
Reply With Quote
 
 
 
 
Joe Pfeiffer
Guest
Posts: n/a
 
      01-17-2013
fl <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Shao Miller
Guest
Posts: n/a
 
      01-17-2013
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
 
Reply With Quote
 
David Thompson
Guest
Posts: n/a
 
      02-04-2013
On Thu, 17 Jan 2013 10:58:53 -0700, Joe Pfeiffer
<(E-Mail Removed)> wrote:

> fl <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making an array wrap, where last index + 1 = first index Shawn W_ Ruby 5 09-16-2009 02:45 PM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
radiobuttonlist: omit label when .DataBind() hellrazor ASP .Net 2 05-17-2004 04:35 PM
Please illustrate how to omit null field from a DB using a datalist. Troy ASP .Net 1 08-26-2003 07:03 PM



Advertisments