Re: [dev] [st] [patch] misplaced parenthesis in LEN macro

From: sin <sin_AT_2f30.org>
Date: Sun, 20 Apr 2014 17:47:11 +0100

On Sun, Apr 20, 2014 at 04:07:12PM +0400, noname_AT_inventati.org wrote:
> On Sun, Apr 20, 2014 at 01:53:33PM +0200, Alexander Huemer wrote:
> > Hi,
> >
> > On Sun, Apr 20, 2014 at 03:41:40PM +0400, noname_AT_inventati.org wrote:
> > > […]
> > > -#define LEN(a) (sizeof(a) / sizeof(a[0]))
> > > +#define LEN(a) (sizeof(a) / sizeof(a)[0])
> > > […]
> >
> > Why parenthesis anyway? a[0] is an expression, not a type, and there is
> > nothing to group here. sizeof is not a function!
>
> Because it is a macro and there can be another expression (such as m+1
> where m is an array). Macro would expand to
> (sizeof(m+1) / sizeof(m+1[0]))
> instead of
> (sizeof(m+1) / sizeof(m+1)[0])

Yup.

#define LEN(x) (sizeof (x) / sizeof *(x))

that's more readable imho.
Received on Sun Apr 20 2014 - 18:47:11 CEST

This archive was generated by hypermail 2.3.0 : Sun Apr 20 2014 - 19:00:03 CEST