On Mon, Jun 1, 2020 at 4:45 PM Bob Proulx <[email protected]> wrote: > Paul Eggert wrote: > > * src/echo.c (main): Don’t assign pointer to bool. > > This is well-defined in C99, but is arguably bad style > > and Oracle Studio 12.6 complains. > ... > > + bool posixly_correct = !!getenv ("POSIXLY_CORRECT"); > > Of course this is fine. But because char *getenv() returns a pointer > it just has this feeling of expressing frustration with the compiler > seeing the !! there. It feels like an obvious cast silencing a > compiler warning. Perhaps that is the intent? :-) > > RETURN VALUE > The getenv() function returns a pointer to the value in the > environment, or NULL if there is no match. > > Just as a soft comment that isn't very strong if it were me I would > use a comparison against a pointer which produced a boolean result and > that boolean result used to assign to a bool. It just feels to me > like more of the types are more obvious this way. And no cast of any > type is either implicit or explicit. > > bool posixly_correct = getenv ("POSIXLY_CORRECT") != NULL;
Hi Bob, I had the same aversion to "!!", ... up until maybe a decade ago when I realized we could view "!!" as a pointer-to-bool "operator". It's an idiom that you have to use a few times before it starts to become natural, then you won't want to go back. Jim
