Re: CALL FOR COMMENT ON NULL: Re: what is typecasting a pointer to the type (void *)p mean?



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



Relevant Pages

  • Re: Is it possible to assign a default value...great, but how?
    ... > much as NULL does it with pointers". ... read your posts in Google Groups but as one reason why people don't normally ... > could call fin the case of a reference argument too. ... void f ...
    (microsoft.public.vc.language)
  • Re: sizeof(ptr) = ?
    ... A void * has the same representation as a char *. ... Char pointers which require additional data ... iff the default offset is 0. ...
    (comp.lang.c)
  • Re: Malloc code
    ... int xxx; ... As for not using the void pointer, I will have to do some further testing ... I just needed some insight on passing arrays of pointers. ... struct MCB *r1; ...
    (microsoft.public.vc.language)
  • Re: polymorphic design approach
    ... > polymorphism. ... * 'Device' pointers are stored so as to allow polymorphic ... virtual void handleMessage ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Realloc and pointer arithmetics
    ... through an AVL tree that managed void* data. ... the C standard does not. ... My frist thought was actually to store into the tree pointers to array ...
    (comp.lang.c)