void The Void Function Return Type
The void keyword is overloaded--it has three separate and unrelated
meanings, depending on the context in which it appears. It can be
used to type a function; as a function prototype; and to define a
generic pointer type.
----------------------------- Function Type ------------------------------
The void type applied to functions signifies that the named function
has no return value. It can also be applied as a cast, in which case
it causes the actual return value to be ignored (which is the default
behavior anyway).
Notes: A syntax error is generated if you attempt to use the
value returned from a function declared as void, or if
you try to return a value from a function defined as
having a void type.
If a function's return value is not used, it is ignored,
so the void cast above is superfluous. It simply
documents the default behavior.
C does not require a non-void function to have a return
value. If a non-void function does have a return value,
C does not require that the value to be used.
-------------------------------- Example ---------------------------------
void clear_screen(); /* function declaration */
void clear_screen() /* function definition */
{
...
}
(void) printf("Hello there.\n");
-------------------------- Function Prototypes ---------------------------
Used as the lone formal argument list specifier in a function
declaration, `void' indicates that the function has no arguments.
Notes: If this declaration is in scope when getfpvalue is
called, the compiler can check that no arguments are
passed in the call.
-------------------------------- Example ---------------------------------
[external] double getfpvalue(void);
------------------------ Generic Pointer Type ----------------------------
In certain cases, it is useful to have a generic pointer type. This
is done using the type void *. A void pointer is large enough to
represent a pointer to any data type. A data pointer can be moved
into and out of a void pointer without loss of information.
Notes: You cannot dereference an object pointed to by a void
pointer, since the compiler does not know the size or
representation of the object being pointed to--it only
knows its address. You must first assign it to some other
data pointer type.
While a pointer to type T can safely be copied into a
void pointer and back again without loss of information,
do not store a pointer to T in a void pointer and read it
out as a type other than T. In the example below, a char
pointer is copied to pv, which is then copied to a double
pointer. Since char and double pointers may have
different attributes, this may generate an error.
-------------------------------- Example ---------------------------------
extern int test(void *);
void *pv;
char *pc;
double *pd;
pv = pc;
pc = pv; /* OK */
pv = pc;
pd = pv; /* potential problem */
test(pv); test(pc);
Seealso:
This page last updated on Fri Nov 30 10:48:32 MSK 2001
Copyright © 1992-2001, Vitaly Filatov, Moscow, Russia
Webmaster