what is typecasting a pointer to the type (void *)p mean?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keith Thompson

    #16
    Re: CALL FOR COMMENT ON NULL: Re: what is typecasting a pointer tothe 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@gmail. com> writes:
    [...][color=blue]
    > The null value for pointers is only useful for setting pointers that
    > haven't been initialised to a *real* address.[/color]

    It's not really about initialization. It's perfectly legitimate to
    assign the value NULL to a pointer that currently points to some
    object.
    [color=blue]
    > 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.[/color]

    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.
    [color=blue]
    > 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.[/color]

    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.
    [color=blue]
    > Consider:
    >
    > void DoSomthing(void );[/color]

    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_Keit h) kst-u@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.

    Comment

    • Jordan Abel

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

      On 2006-01-26, Lucien Kennedy-Lamb <lucien0@gmail. com> wrote:[color=blue]
      > 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.[/color]

      not quite. NULL's representation may not be all bits zero, but a
      constant 0 assigned to a pointer must be a null pointer.
      [color=blue]
      > 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.[/color]

      Your interpretation of the standard is incorrect.

      Comment

      Working...