lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Wed, Apr 04, 2007 at 03:25:56PM -0300, Roberto Ierusalimschy wrote:
> > Unlike 0, the result of a comma expression has a type, int in this
> > case, and int cannot be assigned to a pointer in C++, or without
> > warnings in C (, not sure if its a bug.
> That makes sense. What surprised me is that the construction does not
> generate even a warning in C.

That's because the type of the expression:


depends what NULL expands to.  In C this is implementation-defined.
gcc is probably doing:

  #define NULL ((void*)0)

which causes the comma expression to have type "pointer to void",
which can be implicitly converted to your return type.

g++ on the other hand defines NULL to be 0 because C++ requires it.
In that case the comma expression is not a pointer.

Bear in mind that in C, any of these are valid implementations:

  #define NULL 0
  #define NULL ((int)0)
  #define NULL (sizeof(char) * (int)3.14159 - ('3' - '0'))

So the fact that it works when compiled with gcc is just luck.  If you
want to make it portable you'll have to cast the NULL to the proper
pointer type.

                                                  -Dave Dodge