Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > why use typecast on NULL?

Reply
Thread Tools

why use typecast on NULL?

 
 
Xiaofeng Ye
Guest
Posts: n/a
 
      12-14-2004
Sombody writes:
HASH_TABLE *hashed_filenames = (HASH_TABLE *)NULL;

why not:
HASH_TABLE *hashed_filenames = NULL;

thanks!
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      12-14-2004

"Xiaofeng Ye" <(E-Mail Removed)> wrote in message
news:cpli09$16n8$(E-Mail Removed)99.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


 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      12-14-2004
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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-14-2004
Xiaofeng Ye <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      12-14-2004
In <cpli09$16n8$(E-Mail Removed)99.com> Xiaofeng Ye <(E-Mail Removed)> 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: (E-Mail Removed)
Currently looking for a job in the European Union
 
Reply With Quote
 
William Ahern
Guest
Posts: n/a
 
      12-14-2004
Dan Pop <(E-Mail Removed)> wrote:
> In <cpli09$16n8$(E-Mail Removed)99.com> Xiaofeng Ye <(E-Mail Removed)> 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;
}
 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      12-14-2004

On Tue, 14 Dec 2004, William Ahern wrote:
>
> Dan Pop <(E-Mail Removed)> 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
 
Reply With Quote
 
William Ahern
Guest
Posts: n/a
 
      12-14-2004
Arthur J. O'Dwyer <(E-Mail Removed)> wrote:
> On Tue, 14 Dec 2004, William Ahern wrote:
> >
> > Dan Pop <(E-Mail Removed)> 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?


See this thread

http://groups-beta.google.com/group/...12342d62b175e8

Specifically, Kevin Easton's remark

result of the comma operator is the type and value of the
_right hand_ expression, not the left.

> (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.)


I use it often to keep my error detection terse. I don't tend to skimp on
error checking and using braces everytime tends to distract from the common
path.

if (a == NULL || b == NULL)
return errno = EINVAL, -1;

And more importantly, that speaks to me more than using two statements. In a
sense what I am actually returning is a new errno *and* -1.

- Bill
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      12-14-2004
>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 <(E-Mail Removed)>,
Arthur J. O'Dwyer <(E-Mail Removed)> 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.
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      12-14-2004
William Ahern wrote:
> ...
> 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
> */
> }
> ...


An expression that contains comma operator or assignment operator is not
a NPC (null pointer constant). Forcing such an expression to pointer
type is not guaranteed to result in NPV (null pointer value). Without a
cast this code is broken. The original one isn't.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why typecast to void when call a function with no return value su C Programming 3 01-23-2009 11:22 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Re: typecast error! Mark Fitzpatrick ASP .Net 1 02-08-2006 04:45 AM
Re: typecast error! Joe Van Meer ASP .Net 1 02-02-2006 02:00 AM



Advertisments