void 在 C、C++ 和 C# 中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1043034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:50:17  来源:igfitidea点击:

What does void mean in C, C++, and C#?

c#c++clanguage-designterminology

提问by Nick Katsivelos

Looking to get the fundamentals on where the term "void" comes from, and why it is called void. The intention of the question is to assist someone who has no C experience, and is suddenly looking at a C-based codebase.

希望了解有关“ void”一词的来源以及为什么将其称为 void的基本原理。这个问题的目的是帮助没有 C 经验的人,并且突然查看基于 C 的代码库。

采纳答案by Gerald

Basically it means "nothing" or "no type"

基本上它的意思是“没有”或“没有类型”

There are 3 basic ways that void is used:

有 3 种使用 void 的基本方式:

  1. Function argument: int myFunc(void)-- the function takes nothing.

  2. Function return value: void myFunc(int)-- the function returns nothing

  3. Generic data pointer: void* data-- 'data' is a pointer to data of unknown type, and cannot be dereferenced

  1. 函数参数:int myFunc(void)-- 函数什么都不带。

  2. 函数返回值:void myFunc(int)-- 函数什么都不返回

  3. 通用数据指针:void* data--'data' 是指向未知类型数据的指针,不能解除引用

Note: the voidin a function argument is optional in C++, so int myFunc()is exactly the same as int myFunc(void), and it is left out completely in C#. It is always required for a return value.

注意:voidin a function 参数在 C++ 中是可选的,因此int myFunc()与 完全相同int myFunc(void),在 C# 中完全省略。返回值始终需要它。

回答by sharptooth

It means "no value". You use voidto indicate that a function doesn't return a value or that it has no parameters or both. Pretty much consistent with typical uses of word voidin English.

意思是“没有价值”。您可以使用void以指示函数不返回一个值,或者说,它没有参数或两者兼而有之。与英语中void一词的典型用法非常一致。

回答by Charlie

In c# you'd use the void keyword to indicate that a method does not return a value:

在 c# 中,您将使用 void 关键字来指示方法不返回值:

public void DoSomeWork()
{
//some work
}

回答by SO User

Void is used only in method signatures. For return types it means method will not return anything to the calling code. For parameters it means, no parameters are passed to the method

void 仅用于方法签名。对于返回类型,这意味着方法不会向调用代码返回任何内容。对于参数,这意味着没有参数传递给方法

e.g.

例如

void MethodThatReturnsAndTakesVoid(void)
{
// Method body
}

In C# we can omit the void for parameters and can write the above code as:

在 C# 中,我们可以省略参数的 void,并且可以将上面的代码编写为:

void MethodThatReturnsAndTakesVoid()
{
// Method body
}

Void should not be confused with null.Null means for the variable whose address is on stack, the value on the heap for that address is empty.

Void 不应与 null 混淆。Null 意味着对于地址在堆栈上的变量,该地址在堆上的值是空的。

回答by Rik

It indicates the absence of a return value in a function.

它表示函数中没有返回值。

Some languages have two sorts of subroutines: procedures and functions. Procedures are just a sequence of operations, whereas a function is a sequence of operations that return a result.

有些语言有两种子程序:过程和函数。过程只是一系列操作,而函数是返回结果的一系列操作。

In C and its derivatives, the difference between the two is not explicit. Everything is basically a function. the voidkeyword indicates that it's not an "actual" function, since it doesn't return a value.

在 C 及其衍生物中,两者之间的区别并不明确。一切基本上都是一个函数。该void关键字表明它不是一个“实际”的功能,因为它没有返回值。

回答by Hannoun Yassir

void mean that you won't be returning any value form the function or method

void 意味着您不会从函数或方法中返回任何值

回答by Nakul Chaudhary

Void means no value required in return type from a function in all of three language.

无效意味着所有三种语言的函数的返回类型都不需要值。

回答by Robert S. Barnes

There are two ways to use void:

有两种使用void的方法:

void foo(void);

or

或者

void *bar(void*);

The first indicates that no argument is being passed or that no argument is being returned.

第一个表示没有传递任何参数或没有返回任何参数。

The second tells the compiler that there is no type associated with the data effectively meaning that the you can't make use of the data pointed to until it is cast to a known type.

第二个告诉编译器没有与数据相关联的有效类型,这意味着您不能使用指向的数据,直到它被转换为已知类型。

For example you will see void*used a lot when you have an interface which calls a function whose parameters can't be known ahead of time.

例如,void*当您有一个接口调用其参数无法提前知道的函数时,您会看到它被大量使用。

For example, in the Linux Kernel when deferring work you will setup a function to be run at a latter time by giving it a pointer to the function to be run and a pointer to the data to be passed to the function:

例如,在 Linux Kernel 中,当推迟工作时,您将设置一个稍后运行的函数,方法是为其提供一个指向要运行的函数的指针和一个指向要传递给该函数的数据的指针:

struct _deferred_work {
sruct list_head mylist;
.worker_func = bar;
.data        = somedata;
} deferred_work;

Then a kernel thread goes over a list of deferred work and when it get's to this node it effectively executes:

然后一个内核线程检查一个延迟工作列表,当它到达这个节点时,它有效地执行:

bar(somedata);

Then in bar you have:

然后在酒吧你有:

void bar(void* mydata) {
    int *data = mydata;
    /* do something with data */;
}

回答by laalto

Three usage cases for void:

void 的三种用法:

  1. Function signatures. void foo(int bar)does not return a value. int bar(void)does not take any parameters but this is usually expressed with empty argument list: int bar(). Usage of the void keyword here corresponds to its meaning in English.

  2. Generic top-type pointer void *that points to unspecified data and cannot be dereferenced. Here the meaning of void is different from other meanings of void: universal type vs. no type.

  3. In casts such as (void) new Foo(this)to signify that the return value is deliberately thrown away. Here the keyword usage also matches its meaning in English.

  1. 函数签名。void foo(int bar)不返回值。int bar(void)不接受任何参数,但这通常用空参数列表表示:int bar()。此处使用 void 关键字与其在英语中的含义相对应。

  2. 指向void *未指定数据且不能取消引用的通用顶级指针。这里 void 的含义与 void 的其他含义不同:通用类型与无类型。

  3. 在诸如(void) new Foo(this)表示故意丢弃返回值之类的强制转换中。这里的关键字用法也符合它在英语中的含义。

Cases 1 and 2 were already covered by @Gerald but case 3 has not been addressed yet.

@Gerald 已经涵盖了案例 1 和案例 2,但案例 3 尚未得到解决。

回答by cyclo

Void is the equivalent of Visual Basic's Sub.

Void 相当于 Visual Basic 的 Sub。