This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

gcc warnings and [in]valid lvals


Hi, all.

Consider the following, where "get_some_string()" may return NULL:

  char *str;
  if( (str = get_some_string()) = NULL)

In "C", you get the expected failure: "invalid lvalue in assignment".
In "C++", since the type info is kept around (or so I think), then this
works, and the char *str is assigned the return value of the function, and
then NULL.  Gcc never complains or warns, regardless of -W levels.

For both "C" and "C++", there is a warning for a situation like this:

  if( str = get_some_string() )

If compiled with -Wall, you get "warning: suggest parentheses around
assignment used as truth value".  For some reason it doesn't show up in
the other test case.

It seems to me that the first case (which does not generate a warning),
while valid, is more often the result of a typo than intent.

Is this a bug in the warnings code, or expected behavior?

Thanks,
GP


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]