void

WBahn

Joined Mar 31, 2012
29,979
thanks very much ,can you please simplify for me by like example i am new in c language
The key to understanding this is that, unlike mathematical functions, program functions in many/most languages do a lot more than take values as arguments and return values as results. Because they can do other things, it is not always necessary for them to take any values as arguments and/or return values as results.

So we might right a function that takes a single argument, a pointer to a string that is assumed to be someone's name, and that does nothing by print out a greeting message:

Rich (BB code):
void Greetings(char *s)
{
   printf("Greetings, %s\n", s);
}
Notice that there is no return statement. None is needed because no value is being returned.

Now let's say we had a function that doesn't take any arguments but that returns a random number between 0 and 1:

Rich (BB code):
double RandNorm(void)
{
   return (double) rand()/ (double) RAND_MAX;
}
 

ErnieM

Joined Apr 24, 2011
8,377
Some compilers allow you to omit the word void.
That would be any ANSII C compiler.

The void keyword instructs the compiler that "no parameter is being used here and that is what is intended." However, the following is also permitted:

Rich (BB code):
void foo();

...
void foo()
{
  // code here
}
Omitting the parameter void also turns off any parameter checking, a dangerous feature indeed.

A pointer may also be cast a void. A pointer typically points to a certain type of object, such as a character or an integer. A void pointer may point to any type.
 

WBahn

Joined Mar 31, 2012
29,979
A pointer may also be cast a void. A pointer typically points to a certain type of object, such as a character or an integer. A void pointer may point to any type.
While a pointer may be cast as a void pointer (or even declared as one), it can't be cast as a void. These are two very different things. I don't know what casting something as a void would even mean (so perhaps I am missing something -- always possible).
 
Top