Why can I use the void variable?

For example, if I made a variable:

void helloWorld; 

What could I do with this? Since he does not represent anything, at first I thought there was nothing to be done about it. I know that functions use void with no return value, but do variables have a purpose?

- EDIT -

I have found the answer. Now I know that void variables are illegal in programming languages ​​like Java, C ++ and C. Thanks for all your answers!

+7
java c ++ c variables void
source share
5 answers

void variables are not allowed in C / C ++ because the compiler cannot determine their size. void acts only as a list of function arguments (takes no arguments) or returns types (returns nothing).

There is void * , which simply means “pointer of any type” and is a generic type of pointer, but you cannot dereference it.

+9
source share

C99 6.2.6 paragraph 19 says:

The void type contains an empty set of values; it is an incomplete type that cannot be completed.

Also, void in C is a type that has no size. Thus, if you must declare a variable of type void , the compiler will not know how much memory will be allocated for it.
So, you cannot declare a void variable because it is of an incomplete type.

void is only useful when we are talking about pointers ( void* ), since it allows you to declare a generic pointer without specifying a type.

Read this answer provided by Keith Thompson for a more detailed explanation.

+4
source share

In Java, you cannot use void as a variable type.

However you can use void

The Void class is an uninteresting placeholder class for storing a reference to a class object that represents the void Java keyword.

You can do nothing with this except to get a Class object, but this is a static field, so you don't need a reference to the object.

+3
source share

In C, you can distinguish functional parameters from void

 (void)myarg; 

this is only useful for resolving warnings about unused parameters.

+1
source share

I think you do not know what is "emptiness" ??? you..

small and quick void info

void is not a data type of type int or char, which is used to declare variables.

void is the return type

The void return type is used to say that we are not returning anything from this function.

if you are not returning anything, you need to write void as the return type for the function (including the main function). alternatively you can say ....

 return 0; 

at the end of the function definition.

Hope this helps you understand what is empty.

0
source share

All Articles