空变量可以用来做什么?

时间:2022-07-13 18:04:32

For example, if I made the variable:

例如,如果我做了变量:

void helloWorld;

What could I do with it? Since it represents nothing, at first I thought that nothing could be done with it. I am aware that functions use void for no return value, but do the variables have a purpose?

我能用它做什么呢?因为它什么也不代表,起初我以为它什么也不能做。我知道函数在没有返回值的情况下使用void,但是变量有目的吗?

-- EDIT --

——编辑

I have found out the answer. I am now aware that void variables are illegal in programming languages such as Java, C++ and C. Thanks for all of your answers!

我找到了答案。我现在意识到void变量在Java、c++和C等编程语言中是非法的。

5 个解决方案

#1


9  

void variables are invalid in C/C++ because the compiler can not determine their size. void is only valid as function argument list (takes no arguments) or return types (returns nothing).

void变量在C/ c++中无效,因为编译器不能确定它们的大小。void仅作为函数参数列表(不带参数)或返回类型(不返回任何值)有效。

There is void * which just means "any type pointer" and is a generic pointer type, but you are not allowed to dereference it.

有void *,它的意思是“任何类型的指针”,是一个通用的指针类型,但是不允许取消引用。

#2


4  

C99 6.2.6 paragraph 19 says:

C99 6.2.6第19段说:

The void type comprises 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 were to declare a variable of type void, the compiler would not know how much memory to allocate for it.
So, you can't declare a void variable because it is of incomplete type.

而且,C中的void是一种没有大小的类型。因此,如果您声明一个类型为void的变量,编译器将不知道要为它分配多少内存。因此,您不能声明一个void变量,因为它是不完整的类型。

void is useful just when we're talking about pointers (void*) since it allows you to declare a generic pointer without specifying the type.

当我们讨论指针(void*)时,void是有用的,因为它允许您在不指定类型的情况下声明一个通用指针。

Read this answer given by Keith Thompson for more detailed explanation.

请阅读Keith Thompson给出的答案,以获得更详细的解释。

#3


3  

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

在Java中,不能将void用作变量类型。

You can however use Void

您可以使用Void

The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.

Void类是一个不实例化的占位符类,用于保存对表示Java关键字Void的类对象的引用。

You won't be able to do anything with it, except get the Class object, but that is a static field anyway so you don't need an object reference.

你将无法用它做任何事情,除了得到类对象,但这是一个静态字段,所以你不需要对象引用。

#4


1  

In C you can cast function parameters to void

在C中,可以将函数参数转换为void

(void)myarg;

this is only useful for dealing with unused parameter warnings however.

但是,这只对处理未使用的参数警告有用。

#5


0  

i think you don't know what 'void' is??? do you..

我想你不知道‘虚空’是什么???你. .

small and quick info about void

关于void的简短信息

void is not a data-type like int or char that you use to declare variables.

void不是用于声明变量的数据类型,比如int或char。

void is a return-type

空白是一个返回类型

void return type is used to say that we are returning nothing from this function.

void返回类型用来表示我们没有从这个函数返回任何东西。

if you don't return anything then you have to write void as return-type for a function (including main function). alternatively you can say ....

如果您不返回任何内容,那么您必须将void写成函数的返回类型(包括主函数)。或者你可以说....

return 0;

at the end of function's definition.

在函数的定义结束时。

i hope this might helped you to understand what is void.

我希望这能帮助你理解什么是无效的。

#1


9  

void variables are invalid in C/C++ because the compiler can not determine their size. void is only valid as function argument list (takes no arguments) or return types (returns nothing).

void变量在C/ c++中无效,因为编译器不能确定它们的大小。void仅作为函数参数列表(不带参数)或返回类型(不返回任何值)有效。

There is void * which just means "any type pointer" and is a generic pointer type, but you are not allowed to dereference it.

有void *,它的意思是“任何类型的指针”,是一个通用的指针类型,但是不允许取消引用。

#2


4  

C99 6.2.6 paragraph 19 says:

C99 6.2.6第19段说:

The void type comprises 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 were to declare a variable of type void, the compiler would not know how much memory to allocate for it.
So, you can't declare a void variable because it is of incomplete type.

而且,C中的void是一种没有大小的类型。因此,如果您声明一个类型为void的变量,编译器将不知道要为它分配多少内存。因此,您不能声明一个void变量,因为它是不完整的类型。

void is useful just when we're talking about pointers (void*) since it allows you to declare a generic pointer without specifying the type.

当我们讨论指针(void*)时,void是有用的,因为它允许您在不指定类型的情况下声明一个通用指针。

Read this answer given by Keith Thompson for more detailed explanation.

请阅读Keith Thompson给出的答案,以获得更详细的解释。

#3


3  

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

在Java中,不能将void用作变量类型。

You can however use Void

您可以使用Void

The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.

Void类是一个不实例化的占位符类,用于保存对表示Java关键字Void的类对象的引用。

You won't be able to do anything with it, except get the Class object, but that is a static field anyway so you don't need an object reference.

你将无法用它做任何事情,除了得到类对象,但这是一个静态字段,所以你不需要对象引用。

#4


1  

In C you can cast function parameters to void

在C中,可以将函数参数转换为void

(void)myarg;

this is only useful for dealing with unused parameter warnings however.

但是,这只对处理未使用的参数警告有用。

#5


0  

i think you don't know what 'void' is??? do you..

我想你不知道‘虚空’是什么???你. .

small and quick info about void

关于void的简短信息

void is not a data-type like int or char that you use to declare variables.

void不是用于声明变量的数据类型,比如int或char。

void is a return-type

空白是一个返回类型

void return type is used to say that we are returning nothing from this function.

void返回类型用来表示我们没有从这个函数返回任何东西。

if you don't return anything then you have to write void as return-type for a function (including main function). alternatively you can say ....

如果您不返回任何内容,那么您必须将void写成函数的返回类型(包括主函数)。或者你可以说....

return 0;

at the end of function's definition.

在函数的定义结束时。

i hope this might helped you to understand what is void.

我希望这能帮助你理解什么是无效的。