Re: Void typecasting

Discussion in 'C Programming' started by James Kuyper, May 27, 2014.

  1. James Kuyper

    James Kuyper Guest

    It is not "best practice", though some people feel otherwise. It makes
    no more sense to type

    (void) strcpy(s,t);

    than it does to type

    (void)(s = t);

    The function call and the assignment statement are both expressions that
    return a value, where that value is ignored.
    None whatsoever. Those who advocate for this approach are not concerned
    with the practical effects of such code, of which there are none. They
    are concerned with it's documentary value - they feel it's important to
    let people reading the code to know that ignoring the return value was
    deliberate, not a mistake.
    NULL is the name a C standard macro, null is an adjective. C is a
    case-sensitive language, you need to keep the distinction between NULL
    and null clear. The macro expands to a null pointer constant (NPC).
    There are many different ways to write an NPC, you don't have to use
    NULL. The expressions 0, (void*)0, '\005' - 5UL are all different ways
    you write NPCs. Any NPC (not just NULL), if converted to a pointer
    type, results in a null pointer value. In many contexts, an NPC gets
    implicitly converted to a null pointer value.

    "NULL pointer" is meaningless, "null pointer" means a pointer value that
    points at no real object or function. It's useful as a way of indicating
    that a pointer is currently not being used to point at anything.
    "pointer to NULL" and "pointer to null" are also both meaningless phrases.
    James Kuyper, May 27, 2014
    1. Advertisements

  2. James Kuyper

    Stefan Ram Guest

    An assignment statement is not an expression.

    An assignment expression is an expression.

    An (assignment )expression does not "return" a value.

    An (assignment )expression /has/ a value.

    (More precisely: an evaluation of an expression yields a value.)
    Stefan Ram, May 27, 2014
    1. Advertisements

  3. James Kuyper

    James Kuyper Guest

    Correct. I should have said:

    "The function call and the assignment are both expressions that have a
    value which is ignored".

    Sorry for the confusion.
    James Kuyper, May 27, 2014
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.