The
The
The
===================================================
The void data type always represents an empty set of values. The only object that can be declared with the type specifier void is a pointer.
When a function does not return a value, you should use void as the type specifier in the function definition and declaration. An argument list for a function taking no arguments is void.
You cannot declare a variable of type void, but you can explicitly convert any expression to type void. The resulting expression can only be used as one of the following:
In the following example, the function find_max is declared as having type void.
void
type is an incomplete type that cannot be
completed.
The
void
type has three important uses:
- To signify that a function returns no value
- To indicate a generic pointer (one that can point to any type object)
- To specify a function prototype with no arguments
void
is used to define
a function, with no parameters, that does not return a value:
void message(void) { printf ("Stop making sense!"); }The next example shows a function prototype for a function that accepts a pointer to any object as its first and second argument:
void memcopy (void *dest, void *source, int length);A pointer to the
void
type has the same representation
and alignment requirements as a pointer to a character type.
The void *
type is a derived type based on
void
.
The
void
type can also be used in a cast expression to
explicitly discard or ignore a value. For example:
int tree(void); void main() { int i; for (; ; (void)tree()){...} /* void cast is valid */ for (; (void)tree(); ;){...} /* void cast is NOT valid, because the */ /* value of the second expression in a */ /* for statement is used */ for ((void)tree(); ;) {...} /* void cast is valid */ }A
void
expression has no value, and cannot be used in
any context where a value is required.===================================================
The void data type always represents an empty set of values. The only object that can be declared with the type specifier void is a pointer.
When a function does not return a value, you should use void as the type specifier in the function definition and declaration. An argument list for a function taking no arguments is void.
You cannot declare a variable of type void, but you can explicitly convert any expression to type void. The resulting expression can only be used as one of the following:
- An expression statement
- The left operand of a comma expression
- The second or third operand in a conditional expression.
In the following example, the function find_max is declared as having type void.
- Note:
- The use of the sizeof operator in the line find_max(numbers, (sizeof(numbers) / sizeof(numbers[0]))); is a standard method of determining the number of elements in an array.
/** ** Example of void type **/ #include/* declaration of function find_max */ extern void find_max(int x[ ], int j); int main(void) { static int numbers[ ] = { 99, 54, -102, 89}; find_max(numbers, (sizeof(numbers) / sizeof(numbers[0]))); return(0); } void find_max(int x[ ], int j) { /* begin definition of function find_max */ int i, temp = x[0]; for (i = 1; i < j; i++) { if (x[i] > temp) temp = x[i]; } printf("max number = %d\n", temp); } /* end definition of function find_max */
No comments:
Post a Comment
Thank you