Why does it omit the first array index in declaration?

F

fl

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,
 
J

Joe Pfeiffer

fl said:
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.
 
S

Shao Miller

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);
}
 
D

David Thompson

fl said:
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 */
{
....
}
}
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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top