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 : 25. Jan 2025, 01:57:25
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20250124165243.678@kylheku.com>
References : 1 2 3 4 5
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2025-01-24, Scott Lurndal <
scott@slp53.sl.home> wrote:
Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2025-01-24, Scott Lurndal <scott@slp53.sl.home> wrote:
You can define
>
#define arraysize (x) (sizeof (x) / sizeof ((x)[0))
>
You can, but you don't need to.
The repetition in things like:
sizeof foo->bar.buf / *sizeof foo->bar.buf
is just irksome. Why do I have to say that thing twice,
to get its number of elements?
Often readability suffers
when you use macros, not to mention the other quirks of
C macro use (in C++, a constexpr function might be
suitable, but the naming being arbitrary (e.g. arraysize,
NUM_ELEMENTS, SIZE, et alia) doesn't aid in readability
or maintainability.
The naming being arbitrary is the argument for standardizing the name
for the macro and sticking it into, for instance, <stddef.h>.
If we didn't have offsetof there, we might have to deal with
OFFSETOF, offsetof, offset, member_offset, and others.
The containerof name for a popular macro seems to be a de facto
standard, luckily.
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca