By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,534 Members | 2,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,534 IT Pros & Developers. It's quick & easy.

why use typecast on NULL?

P: n/a
Sombody writes:
HASH_TABLE *hashed_filenames = (HASH_TABLE *)NULL;

why not:
HASH_TABLE *hashed_filenames = NULL;

thanks!
Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

"Xiaofeng Ye" <ch***@163.com> wrote in message
news:cp***********@mail.cn99.com...
Sombody writes:
HASH_TABLE *hashed_filenames = (HASH_TABLE *)NULL;

why not:
HASH_TABLE *hashed_filenames = NULL;


Either way is valid. The former might be considered by some to be more
descriptive. It's a 'style' issue.

-Mike
Nov 14 '05 #2

P: n/a
Xiaofeng Ye wrote:
Sombody writes:
HASH_TABLE *hashed_filenames = (HASH_TABLE *)NULL;

why not:
HASH_TABLE *hashed_filenames = NULL;
...


There's no formal reason. Might be some weird coding standard or
personal preference.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #3

P: n/a
Xiaofeng Ye <ch***@163.com> writes:
Sombody writes:
HASH_TABLE *hashed_filenames = (HASH_TABLE *)NULL;

why not:
HASH_TABLE *hashed_filenames = NULL;


No good reason at all. The latter is simpler and clearer.
Casts should be used only when they're actually necessary.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4

P: n/a
In <cp***********@mail.cn99.com> Xiaofeng Ye <ch***@163.com> writes:
Sombody writes:
HASH_TABLE *hashed_filenames = (HASH_TABLE *)NULL;

why not:
HASH_TABLE *hashed_filenames = NULL;


There is exactly one instance when NULL needs to be cast: when passed as
argument to a variadic function (or a non-prototyped function, but there
is no good reason for not prototyping your functions).

Otherwise, the compiler has enough context information to figure out to
what type the null pointer constant needs to be converted and the
conversion is automatic.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #5

P: n/a
Dan Pop <Da*****@cern.ch> wrote:
In <cp***********@mail.cn99.com> Xiaofeng Ye <ch***@163.com> writes:
Sombody writes:
HASH_TABLE *hashed_filenames = (HASH_TABLE *)NULL;

why not:
HASH_TABLE *hashed_filenames = NULL;

There is exactly one instance when NULL needs to be cast: when passed as
argument to a variadic function (or a non-prototyped function, but there
is no good reason for not prototyping your functions).


On OpenBSD NULL is defined as simply `0'. And int's are 32-bits while
pointers 64-bits. The second scenario goes something like:

#include <stdlib.h>
#include <errno.h>

struct something;

struct something *func(void) {
return errno = EINVAL, NULL; /* Should be (struct something *)0
* or (void *)0 or (void *)NULL
* or (struct something *)NULL
*/
}

int main(void) {
func();

return 0;
}
Nov 14 '05 #6

P: n/a

On Tue, 14 Dec 2004, William Ahern wrote:

Dan Pop <Da*****@cern.ch> wrote:
There is exactly one instance when NULL needs to be cast: when passed as
argument to a variadic function (or a non-prototyped function, but there
is no good reason for not prototyping your functions).


On OpenBSD NULL is defined as simply `0'. And int's are 32-bits while
pointers 64-bits. The second scenario goes something like:

#include <stdlib.h>
#include <errno.h>

struct something;

struct something *func(void) {
return errno = EINVAL, NULL; /* Should be (struct something *)0
* or (void *)0 or (void *)NULL
* or (struct something *)NULL
*/


Is this right? I was all set to pounce on it as incorrect, but
then I hesitated. Is there something magic about the comma operator
in this example? Obviously

return NULL;

is fine --- that's a pointer context, and 'NULL' is converted correctly.
Is William's example actually valid, and if so, why?

(BTW, I would say that's not really a place for casting NULL even if
William is right. I'd rewrite it as
errno = EINVAL; return NULL;
No casts necessary.)

-Arthur
Nov 14 '05 #7

P: n/a
>On Tue, 14 Dec 2004, William Ahern wrote:
struct something *func(void) {
return errno = EINVAL, NULL; /* Should be (struct something *)0
* or (void *)0 or (void *)NULL
* or (struct something *)NULL
*/

In article <Pi***********************************@unix46.andr ew.cmu.edu>,
Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> wrote: Is this right? I was all set to pounce on it as incorrect, but
then I hesitated. Is there something magic about the comma operator
in this example? Obviously

return NULL;

is fine --- that's a pointer context, and 'NULL' is converted correctly.
Is William's example actually valid, and if so, why?
I believe so, yes. The problem is that comma-expressions are not
constant-expressions, according to the Standard. If NULL is defined
as 0 -- which is of course legitimate -- then this line expands to
something like:

return errno = 42, 0;

A comma-expression is not a constant, not even when both its operands
are constants, as in (3,4). (Our expression in question has a
side-effect on the left-hand side, so it would be still be "not a
constant" according to such a rule, not that either C standard has
such a rule.) Since a null pointer constant is obtained only with
an integral *constant* expression with value 0 (possibly with an
attached cast to (void *)), and this zero is not constant, this
must not be a null pointer constant.
(BTW, I would say that's not really a place for casting NULL even if
William is right. I'd rewrite it as
errno = EINVAL; return NULL;
No casts necessary.)


Yes, this is much cleaner for a future maintainer.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #8

P: n/a

On Tue, 14 Dec 2004, William Ahern wrote:

Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> wrote:
On Tue, 14 Dec 2004, William Ahern wrote:
Dan Pop <Da*****@cern.ch> wrote:
There is exactly one instance when NULL needs to be cast: when passed as
argument to a variadic function (or a non-prototyped function, but there
is no good reason for not prototyping your functions).

struct something *func(void) {
return errno = EINVAL, NULL;


Is this right? I was all set to pounce on it as incorrect, but
then I hesitated. Is there something magic about the comma operator
in this example?


Ah yes, the comma operator cannot be used in constant expressions. So
we end up trying to convert the (non-constant-expression) integer 0 to
a pointer type, and failing. You're right. Thanks.

Re: readability, I still maintain that you can have your cake and eat
it too. If I really ended up writing this sort of thing a lot (which I
don't, since I don't use 'errno' to report errors in my own code), I'd
probably use a macro like

#define RETURN_ERRNO(x,y) do { errno=(x); return y; } while (0)

(Pedantic treat: I'm not happy with the macro's name, since it takes a
return value and an errno value, but not in that order. But if I called
it 'ERRNO_RETURN', it would invade the implementation's namespace. And
if I simply reversed the parameters, it would use them in the opposite
order from how they were passed, which seems unintuitive. Quite the
dilemma! :)

-Arthur
Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.