Why does void in C mean not void?

Posted by Naftuli Tzvi Kay on Programmers See other posts from Programmers or by Naftuli Tzvi Kay
Published on 2014-08-22T20:33:11Z Indexed on 2014/08/22 22:31 UTC
Read the original article Hit count: 367

Filed under:
|
|

In strongly-typed languages like Java and C#, void (or Void) as a return type for a method seem to mean:

This method doesn't return anything. Nothing. No return. You will not receive anything from this method.

What's really strange is that in C, void as a return type or even as a method parameter type means:

It could really be anything. You'd have to read the source code to find out. Good luck. If it's a pointer, you should really know what you're doing.

Consider the following examples in C:

void describe(void *thing)
{
    Object *obj = thing;
    printf("%s.\n", obj->description);
}

void *move(void *location, Direction direction)
{
    void *next = NULL;

    // logic!

    return next;
}

Obviously, the second method returns a pointer, which by definition could be anything.

Since C is older than Java and C#, why did these languages adopt void as meaning "nothing" while C used it as "nothing or anything (when a pointer)"?

© Programmers or respective owner

Related posts about java

Related posts about c#