Deutsch English Français Italiano |
<vt5qld$inuo$6@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: do { quit; } else { } Date: Wed, 9 Apr 2025 14:54:05 +0200 Organization: A noiseless patient Spider Lines: 39 Message-ID: <vt5qld$inuo$6@dont-email.me> References: <vspbjh$8dvd$1@dont-email.me> <8634enhcui.fsf@linuxsc.com> <vsph6b$ce6m$5@dont-email.me> <86ldsdfocs.fsf@linuxsc.com> <20250406161323.00005809@yahoo.com> <86ecy5fjin.fsf@linuxsc.com> <20250406190321.000001dc@yahoo.com> <86plhodtsw.fsf@linuxsc.com> <20250407210248.00006457@yahoo.com> <vt15lq$bjs0$3@dont-email.me> <vt2lp6$1qtjd$1@dont-email.me> <vt31m5$2513i$1@dont-email.me> <slrnvvcdpa.jbc.ike@iceland.freeshell.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 09 Apr 2025 14:54:06 +0200 (CEST) Injection-Info: dont-email.me; posting-host="39048323a720df50869558e92437b3ec"; logging-data="614360"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+XpJJqRxz3wIC8UHXa4JTX+KUghvWtTD0=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Cancel-Lock: sha1:UWXwSkFBXc1UbhG5198TXRFf0kc= In-Reply-To: <slrnvvcdpa.jbc.ike@iceland.freeshell.org> Content-Language: en-GB Bytes: 2821 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]);