Sujet : Re: do { quit; } else { }
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.cDate : 09. Apr 2025, 13:54:05
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vt5qld$inuo$6@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0
On 09/04/2025 11:00, Ike Naar wrote:
On 2025-04-08, bart <bc@freeuk.com> wrote:
However if I need to initialise the variable:
>
extern int table[]; // shared
int table[] = (10, 20, 30)
>
then other modules can't pick up length of the array.
extern int table[3];
That is fine if you know the size in advance of initialisation. Very often, of course, you /do/ know the size - especially when talking about data shared directly between translation units. (And if it is not shared, you don't declare it in a header.)
An array really needs two bits of information - a way to find the first element, and the number of elements. (There are also other bits of compile-time data such as the type.) In C, these two things are not tightly joined - you typically have to pass them separately when exchanging information about an array. This means you get maximal efficiency - you don't need to pass around information that you have no use of or know from somewhere else. But it also means you need a little extra effort in the code when you need them both.
For example, you can have :
// unit.h
extern int table[];
extern const int table_count;
// unit.c
#include "unit.h"
int table = [10, 20, 30};
const int table_count = sizeof(table) / sizeof(table[0]);