Le 10/06/2013 10:15, al_bin a écrit :
> Yes, number in second [] is necessery. Why?

Here is what I think, which may or may not be correct:

Because in C a 2-dimensional array is defined as an array of arrays.
Which is the same as a pointer to array.
And you can't have pointer to undefined types, so only the first dimension is
optional.

A quick test with gcc for the following code:
int a[][] = {{1,1},{2,2},{3,3} };

tells:
test.c:1: error: array type has incomplete element type

so that seems to confirm my suggestion with one precision: you can't declare
arrays when the *element type* is unknown.

---

A compiler extension could count the number of initializer entries and use that,
but there is no guarantee that the number of initializers will always match the
size of the initialized array.

How is the compiler supposed to deal with that :
int a[][] = {{1},{2,2},{3,3,3} };

If we try to think as a compiler: Okay, this array has 3 "main" elements, but
what is the element size? you need the type to compute that.
Is it int[3] where all elements are not initialized?
or is it int[2] with too many initializers in the third entry? is it something 
else?

This information is required to allocate memory for array elements!

>From the low-level perspective of the C language, it feels safe to prevent such
automatic and ambiguous data definitions.

BR
Sebastien


PS:
On the contrary, you can have pointers to undefined structures, in declarations
such as :

struct node {
    struct node *next;
    int value;
};

you can even have undefined structure *pointers* where the structure is defined
elsewhere:

struct undefined_struct *global_variable;

This will compile, and you can use this global variable as a pointer, but as
soon as you try to dereference the pointer somewhere (with
global_variable->member), you'll get an undefined type error, because the
compiler does not know which members are available.



------------------------------------------------------------------------------
How ServiceNow helps IT people transform IT departments:
1. A cloud service to automate IT design, transition and operations
2. Dashboards that offer high-level views of enterprise services
3. A single system of record for all IT processes
http://p.sf.net/sfu/servicenow-d2d-j
_______________________________________________
Sdcc-user mailing list
Sdcc-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sdcc-user

Reply via email to