Previous Page
Next Page

2.6. The Type void

The type specifier void indicates that no value is available. Consequently, you cannot declare variables or constants with this type. You can use the type void for the purposes described in the following sections.

2.6.1. void in Function Declarations

A function with no return value has the type void. For example, the standard function perror( ) is declared by the prototype:

    void perror( const char * );

The keyword void in the parameter list of a function prototype indicates that the function has no parameters:

    FILE *tmpfile( void );

As a result, the compiler issues an error message if you try to use a function call such as tmpfile("name.tmp"). If the function were declared without void in the parameter list, the C compiler would have no information about the function's parameters, and hence be unable to determine whether the function call is correct.

2.6.2. Expressions of Type void

A void expression is one that has no value. For example, a call to a function with no return value is an expression of type void:

    char filename[ ] = "memo.txt";
    if ( fopen( filename, "r" ) == NULL )
      perror( filename );             // A void expression.

The cast operation (void)expression explicitly discards the value of an expression, such as the return value of a function:

    (void)printf("I don't need this function's return value!\n");

2.6.3. Pointers to void

A pointer of type void * represents the address of an object, but not its type. You can use such quasi-typeless pointers mainly to declare functions that can operate on various types of pointer arguments, or that return a "multipurpose" pointer. The standard memory management functions are a simple example:

    void *malloc( size_t size );
    void *realloc( void *ptr, size_t size );
    void free( void *ptr );

As Example 2-3 illustrates, you can assign a void pointer value to another object pointer type, or vice versa, without explicit type conversion.

Example 2-3. Using the type void
// usingvoid.c: Demonstrates uses of the type void
// -------------------------------------------------------
#include <stdio.h>
#include <time.h>
#include <stdlib.h>  // Provides the following function prototypes:
                     // void srand( unsigned int seed );
                     // int rand( void );
                     // void *malloc( size_t size );
                     // void free( void *ptr );
                     // void exit( int status );

enum { ARR_LEN = 100 };

int main( )
  int i,                                // Obtain some storage space.
      *pNumbers = malloc(ARR_LEN * sizeof(int));

  if ( pNumbers == NULL )
    fprintf(stderr, "Insufficient memory.\n");

  srand( (unsigned)time(NULL) );        // Initialize the
                                        // random number generator.

  for ( i=0; i < ARR_LEN; ++i )
    pNumbers[i] = rand( ) % 10000;          // Store some random numbers.

  printf("\n%d random numbers between 0 and 9999:\n", ARR_LEN );
  for ( i=0; i < ARR_LEN; ++i )         // Output loop:
    printf("%6d", pNumbers[i]);         // Print one number per loop iteration
    if ( i % 10 == 9 ) putchar('\n');   // and a newline after every 10 numbers.
  free( pNumbers );                     // Release the storage space.
  return 0;

Previous Page
Next Page