Re: CALL FOR COMMENT ON NULL: Re: what is typecasting a pointer to the type (void *)p mean?
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Thu, 26 Jan 2006 04:44:26 GMT
Please don't change the subject header when posting a followup without
a very good reason, and please don't use an all-caps subject (it looks
like spam).
"Lucien Kennedy-Lamb" <lucien0@xxxxxxxxx> writes:
[...]
> The null value for pointers is only useful for setting pointers that
> haven't been initialised to a *real* address.
It's not really about initialization. It's perfectly legitimate to
assign the value NULL to a pointer that currently points to some
object.
> Null pointers *must* be assigned using the NULL macro (defined by
> <stdio.h>). The reason is that, while most of the time NULL is defined
> as 0, it may not be on every platform.
>
> char *string = NULL; /* I'm not pointing to anything useful yet */
>
> CALL FOR COMMENT HERE: I've never seen a compiler *not* use 0 as NULL,
> so who has? Who can list an example where this is not the case? I've
> seen so much code that relies on the NULL == 0 assumption.
That's not quite correct.
What code have you seen that relies on NULL == 0? Since a literal 0
is a null pointer constant, they're guaranteed to be equal, but a null
pointer *value* isn't necessarily represented as all-bits-zero. I use
systems where the NULL macro is defined as 0, and others where it's
defined as ((void*)0). This shouldn't make any difference for
well-written code.
Please read section 5 of the C FAQ, available at www.c-faq.com.
> Void...
>
> In the English language "void" means empty, useless or a vacuum.
>
> In C it has quite a different meaning. My best explanation is a type
> of zero length data.
It would be better to say it's a type that doesn't have a size. More
precisely, it's an incomplete type that cannot be completed.
You can't have an object of type void. You can have a
pointer-to-void, but you can't dereference it unless you first convert
it to some pointer-to-object type.
> Consider:
>
> void DoSomthing(void);
The two "void"s here mean different things. The first means that the
function doesn't return a value (or that it returns type "void"). The
second means that the function doesn't take any arguments; it doesn't
refer to the void type. The use in "void*" is arguably a third
distinct meaning. (It's not *quite* as bad as "static").
See section 4 of the C FAQ.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <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.
.
- References:
- what is typecasting a pointer to the type (void *)p mean?
- From: Abhishek
- Re: what is typecasting a pointer to the type (void *)p mean?
- From: Michael Rasmussen
- Re: what is typecasting a pointer to the type (void *)p mean?
- From: Abhishek
- Re: what is typecasting a pointer to the type (void *)p mean?
- From: Emmanuel Delahaye
- Re: what is typecasting a pointer to the type (void *)p mean?
- From: Michael Rasmussen
- Re: what is typecasting a pointer to the type (void *)p mean?
- From: Abhishek
- CALL FOR COMMENT ON NULL: Re: what is typecasting a pointer to the type (void *)p mean?
- From: Lucien Kennedy-Lamb
- what is typecasting a pointer to the type (void *)p mean?
- Prev by Date: Re: CALL FOR COMMENT ON NULL: Re: what is typecasting a pointer to the type (void *)p mean?
- Next by Date: Re: CALL FOR COMMENT ON NULL: Re: what is typecasting a pointer to the type (void *)p mean?
- Previous by thread: Re: CALL FOR COMMENT ON NULL: Re: what is typecasting a pointer to the type (void *)p mean?
- Next by thread: Re: CALL FOR COMMENT ON NULL: Re: what is typecasting a pointer to the type (void *)p mean?
- Index(es):
Relevant Pages
|