Void type

The keyword void (English for " void ", " invalid ", " empty ") is used in some programming languages ​​instead of one data type to indicate that no data is passed or the type of data is not specified. Syntactically, such a data type is void treated, but it can only be in certain places. So it is not possible for example, to declare a variable of type void.

Void as a return type

In the programming languages ​​C, C , Java and C # (called procedures) is void used for functions or methods with no return value. In these languages ​​must syntactically be specified for each function / method a return type; if no value is returned, instead, is void.

The following example defines a method with no return value in Java:

Void hello () {     System.out.println ( "Hello World! "); } void in C and C

In C and C is void syntactically and semantically a base data type. It is the so-called "empty data type" ( engl. empty type) and an "incomplete type". The latter ensures that you can not create objects of type void.

Void is used in these languages ​​for three additional tasks:

Untyped pointer

With a void * pointer is declared without specifying the type of data to which the pointer points ( untyped pointer, pointer to void). An untyped pointer can not be dereferenced or used for pointer arithmetic, but must be converted to a typed pointer by type conversion first.

Function arguments

Functions without arguments should in C with the keyword void in place of the argument list are declared as in the following example:

Void hello (void ) {     printf (" Hello world \ n " ); } This specific case is necessary because there are two different types in C to declare functions. In the original notation (old -style), the types of the arguments are not specified in the function header; the new notation was introduced so that the compiler can check the number and types of arguments in a function call. To ensure compatibility with existing C code, declarations were allowed to continue with the old notation. This function declarations without arguments are not incorrectly identified as old-style, it was determined that in this case, the keyword void in place of the argument list is available.

In C Old-style function declarations, however, are not possible. Therefore, it is void as an argument list is equivalent to an empty argument list.

Type conversion

A cast according void means that the value of the expression is discarded. This can be used to explicitly indicate that a value is not used, for example, a function argument is not needed or not interested in the return value of a function.

An example:

Double shipping costs ( int count, double price) {       (void ) price; / / Have not used       return 2.0 * number; }   void raeume_auf (FILE * f ) {      (void) fclose ( f); / / ( Error ) is return-value care. } The programmer specifies the fact that the argument was really not remember but is not intentionally used. This serves to document the code and possibly to suppress warnings from the compiler.

807710
de