Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: EXIT_SUCCESS Date: Mon, 25 Jul 2016 15:04:44 -0700 Organization: None to speak of Lines: 51 Message-ID: References: <87r3b4xdsa.fsf@gmail.com> <6f1e2608-eac3-49ea-a5f4-0fdc746b2942@googlegroups.com> <579638b1.119218@news.xs4all.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="15913"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19DbAiEYr3T5TP5E2PVpaad" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:RDacgsrqyK+9fDLJ5oq8Ex/7ipA= sha1:rujxS4k/aNrJ4URtqmYs7GJ+UqM= Xref: csiph.com comp.lang.c:91066 BartC writes: > On 25/07/2016 19:14, Richard Heathfield wrote: >> On 25/07/16 19:04, Keith Thompson wrote: >>> Richard Heathfield writes: > >>>>>> size_t maxval = sizeof numbers / sizeof numbers[0] - 1; >>>>>> return numbers[a <= maxval ? a : maxval]; > >>>> A slightly better solution (which, of course, I thought of /after/ >>>> posting) would have been to make maxval static. >>> >>> And const? >> >> Yep. I thought of that one after posting the one I thought of after >> posting the first one. Shades of IEFBR14. > > So: > > static const size_t maxval = ... > > before you've even started expressing the size of the array? What? The size of the array is determined by the initializer. We were merely talking about adding "const" and "static" to the existing declaration of "maxval", like this: static const char *get_foo(size_t a) { static const char *numbers[] = { "zero", "one", "two", "three", "four" }; static const size_t maxval = sizeof numbers / sizeof numbers[0] - 1; return numbers[a <= maxval ? a : maxval]; } [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"