Channels ▼


The Different Meanings of void

The early dialect of C described by Kernighan and Ritchie in The C Programming Language (often called "Classic C") didn't include the keyword void, and therefore had no type void *. Lacking void *, Classic C programs tended to use char * as the type for a "generic" data pointer — a pointer that can point to any data object. Unfortunately, this increased the need to use cast expressions.

For example, Classic C didn't include the now standard memory management functions malloc and free. In Chapter 5 of their book, Kernighan and Ritchie implemented their own allocation function, alloc, which they defined as:

char *alloc(bytes)
unsigned bytes;

This function definition is in the old, non-prototyped style of Classic C. In Standard C, the equivalent declaration would be:

char *alloc(unsigned bytes);

This alloc function returns the address of the allocated storage as a pointer of type char *. However, Ritchie designed alloc to allocate storage for any type of object, not just char or array of char. When allocating storage for something other than a char (or an array thereof), the program must convert the char * result into a pointer to the intended type of the allocated object — a conversion that requires a cast. Kernighan and Ritchie used casts to convert the result of calling alloc to something other than char *, in functions such as:

struct tnode *talloc()
   return ((struct tnode *)alloc(sizeof(struct tnode)));

The Classic C implementation of free posed a similar problem. Kernighan and Ritchie defined their version of free as:

char *ap

Absent the keyword void, they omitted the return type to indicate that the function returns nothing. In truth, the return type defaulted to int. In Standard C, the equivalent declaration would be:

int free(char *ap);

Calling free to deallocate anything other than a char or array of chars required a cast, as in:

int *ip;
free((char *)ip);

Standard C eliminated the need for these casts by embracing void * as the generic data pointer type. For any object type T, a C program can convert a T * to or from a void * without using a cast.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags


<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags


<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

