I have come across this code. Is the use of void an aesthetic issue to indicate that the function has no parameters or does that function really expect something of the void type? Is there a void type (apart from its use to declare a function that returns nothing )?
The word void, can appear as a parameter of a function, to indicate that it does not receive parameters, as a return type, to indicate that the function does not return anything, or as a type of a pointer, as here:
void *p;
This last case is interesting. What can a pointer to "nothing" mean? In this context voidit is not understood as "nothing", but rather as "whatever". The above statement simply declares a pointer, but "doesn't want" to go into the details of what's in the pointed place.
Such a pointer could later be used to point to an integer, or a real, or a character, whatever. To allocate memory for it would be used malloc()normally, and the number of bytes to allocate depends on how we intend to use it later. For example, if I want to store a floatat the address pointed to by I pwill need to reserve the number of bytes that a occupies float:
p = malloc(sizeof(float));
But to later assign a value to it, it will be necessary to use a casting since otherwise, if we use it like this:
*p = 3.1415926;
the compiler will give an error saying that a void. pIt needs to be temporarily converted to a pointer to float, like this:
*(float *)p = 3.1415926;
The usefulness of this is mainly in being able to pass a pointer to a type that we don't want to specify at compile time, because the same function could support different types (another function parameter could specify for example how to use the pointer). . Actually, it's still a "dirty" trick to skip types in cases where it can't be done any other way.
Actually the use of void in C is more than aesthetic, consider the following program:
#include <stdio.h>
int incrementa();
int main()
{
int y = incrementa();
printf("Hello world! [%d]\n",y);
return 0;
}
int incrementa(int n) {
return n+1;
}
If you compile, you will notice that it does not mark errors in gcc, however there is a problem in the call to the function, it required a parameter, but the compiler did not detect it, and even created executable code. This is because when no parameters are placed in a function prototype, the compiler assumes that it can receive any number of parameters.
If you try to compile the same code but put the word void in the prototype you will find that the compiler correctly identifies the problem.
The word
void
, can appear as a parameter of a function, to indicate that it does not receive parameters, as a return type, to indicate that the function does not return anything, or as a type of a pointer, as here:This last case is interesting. What can a pointer to "nothing" mean? In this context
void
it is not understood as "nothing", but rather as "whatever". The above statement simply declares a pointer, but "doesn't want" to go into the details of what's in the pointed place.Such a pointer could later be used to point to an integer, or a real, or a character, whatever. To allocate memory for it would be used
malloc()
normally, and the number of bytes to allocate depends on how we intend to use it later. For example, if I want to store afloat
at the address pointed to by Ip
will need to reserve the number of bytes that a occupiesfloat
:But to later assign a value to it, it will be necessary to use a casting since otherwise, if we use it like this:
the compiler will give an error saying that a
void
.p
It needs to be temporarily converted to a pointer tofloat
, like this:The usefulness of this is mainly in being able to pass a pointer to a type that we don't want to specify at compile time, because the same function could support different types (another function parameter could specify for example how to use the pointer). . Actually, it's still a "dirty" trick to skip types in cases where it can't be done any other way.
You are in C++, and, in that language, the use of
void
is to facilitate the reuse of C code. Indicate that a function receivesvoid
This is the same as indicating that the function does not receive any arguments:
This allows you to compile original C language code into a C++ compiler without much trouble.
If you're in C, things change quite a bit: a function
It is, just like in C++, a function that takes no arguments. However, a function with nothing:
Indicates a function that can receive any argument type(s) :
The above code compiles without errors.
Actually the use of void in C is more than aesthetic, consider the following program:
If you compile, you will notice that it does not mark errors in gcc, however there is a problem in the call to the function, it required a parameter, but the compiler did not detect it, and even created executable code. This is because when no parameters are placed in a function prototype, the compiler assumes that it can receive any number of parameters.
If you try to compile the same code but put the word void in the prototype you will find that the compiler correctly identifies the problem.
[1] I pass a different number of parameters than defined, but it compiles and works