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:
free(ap)
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.


