Home > Backend Development > C++ > body text

What does void stand for in c++

下次还敢
Release: 2024-05-01 10:12:15
Original
836 people have browsed it

void means no type in C. It is used for: function return value type: means that the function does not return any value. Function parameter type: Indicates that the function does not accept any parameters. Pointer type: void pointer can point to any type of data, but explicit type conversion is required. Identifies an uninitialized variable. Indicates that the expression does not produce a value.

What does void stand for in c++

The meaning of void in C

void is a special data type in C, which means there is no type . It is usually used for the return value type and formal parameter type of functions, indicating that these functions do not return any value or accept any parameters.

Function return value type

When declaring a function, if void is used as the return value type, it means that the function will not return any value. For example:

<code class="cpp">void print_message() {
  // 打印一条消息
  std::cout << "Hello, world!" << std::endl;
}</code>
Copy after login

Formal parameter type

Similarly, when declaring a function, if void is used as the formal parameter type, it means that the function does not accept any parameters. For example:

<code class="cpp">void swap(int& a, int& b) {
  // 交换两个整数
  int temp = a;
  a = b;
  b = temp;
}</code>
Copy after login

Pointer type

void pointer is a special pointer type that does not point to any specific type of data. It can point to an object of any type, but an explicit type conversion must be done first. For example:

<code class="cpp">int* ptr = new int;  // 分配一个int型变量
void* void_ptr = ptr;  // 将int指针转换为void指针</code>
Copy after login

Other uses

void can also be used in other situations, such as:

  • Identifies uninitialized variables
  • Indicates that an expression will not produce a value (for example, in the conditional expression of a for loop)

The above is the detailed content of What does void stand for in c++. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c++
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template