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

From: sin <sin_AT_2f30.org>
Date: Sun, 20 Apr 2014 20:13:02 +0100

On Sun, Apr 20, 2014 at 02:43:53PM +0200, FRIGN wrote:
> On Sun, 20 Apr 2014 13:53:33 +0200
> Alexander Huemer <alexander.huemer_AT_xx.vu> 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!
>
> #define LEN(a) (sizeof a / sizeof *a)
>
> is the right way to do it.

You are missing the parentheses there.

Your macro provides the wrong answer for something like:

  int a[] = { 1, 2, 3, 4, 5 };
  printf("%zu\n", LEN(a + 2));

Cheers,
sin
Received on Sun Apr 20 2014 - 21:13:02 CEST

This archive was generated by hypermail 2.3.0 : Sun Apr 20 2014 - 21:24:06 CEST