Sujet : Re: Results of survey re. a new array size operator
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 30. Jan 2025, 23:31:25
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20250130142551.285@kylheku.com>
References : 1 2 3 4 5 6
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2025-01-30, Scott Lurndal <
scott@slp53.sl.home> wrote:
The decades old standard idiom of sizeof(x)/sizeof(x[0])
is self-documenting and requires no macros or new language
features.
I've seen the bug more than once whereby x was a pointer.
That's a threat whether you use a macro or open-code it,
of course.
GCC has developed a diagnostic for this; so whether you have
a macro or write it out as above, you care covered.
If a standard-defined operator or macro were available for this, it
could require a diagnostic in every conforming implementation,
when the argument isn't an array. Implementations that don't
have a diagnostic for sizeof(ptr)/sizeof(ptr[0]) would still
have to diagnose countof(ptr), or whatever it is called.
That would be a benefit in addition to saving keystrokes.
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca