C++ Void 指针


2022年9月15日, Learn eTutorial
2209

本教程主要关注 C++ 中的 void 指针,以及如何通过一些示例来使用它们。

在继续本教程之前,请查看 C++ 指针

C++ 中的 Void 指针

  • 任何数据类型的地址都可以存储在 void 指针中,它是一种通用的指针,没有特定的数据类型关联。
  • 没有关联数据类型的指针被称为 void 指针。
  • 通用指针被称为“void 指针”,它可以强制转换为任何类型,也可以保存任何类型的地址。
  • 指针被标记为 void 并不一定意味着它不指向任何东西。实际上,它正在指向,但我们不确定它所指向的对象的精确种类或类型。

语法


void *ptr;  
 

在 C++ 中,不能将特定数据类型变量的地址分配给具有不同数据类型的指针。

让我们考虑一个例子


int *ptr; //declaration of  integer pointer 

float a=20.2; // initialization of  floating variable 

ptr= &a;// An error is generated by this statement.

 

在上面的示例中,我们声明了一个名为“a”的浮点变量和一个名为“ptr”的整数类型指针。在声明之后,我们尝试将变量的地址存储在 ptr 中,但是这在 C++ 中是不可能的,因为该变量不能保存不同数据类型的地址。

在本例中,地址是一个 double 类型变量,这就是发生错误的原因。

另一方面,该指针是 int 类型。在这种情况下,可以使用 C++ 中的 void 指针(指向 void 的指针)

例如:


void *ptr; // void pointer is used
                                                                      
double d = 20.2;                                                                      

ptr = &d; // hence it becomes a valid statement
 

void 指针是一种通用指针,当我们在不知道指针所指向变量的数据类型时使用它,正如我们之前在本文中讨论的那样。

示例:现在让我们看看如何在 C++ 中使用 void 指针


#include <iostream>
using namespace std;
int main() {
    void* ptr;
    float f = 5.0f;

    // assign the float address to void
    ptr = &f;

    cout << "Address of floating point no will be: " << &f << endl;
    cout << "Address of void pointer no will be: " << ptr << endl;

    return 0;
}

输出

Address of floating point no will be: 0x7ffca8253a6c
Address of void pointer no will be: 0x7ffca8253a6c

这里,值 &f 被赋值给指针 ptr

输出表明,名为 f 的浮点变量的地址存储在 void 指针 ptr 中。

由于 void 是一个空类型,void 指针不能被解引用。


void* ptr;
float* fptr;
float f = 5.0;
                                                                      
// now assign  the float address to void pointer
ptr = &f;
cout << *ptr << endl;  // an error is occured
//now  assign the  float address to float pointer
fptr = &f;
cout << *fptr << endl;   //the  value is valid
 

示例 2:让我们编写一个 C++ 程序来打印 Void 指针的内容。

静态转换操作主要用于打印 void 指针的内容。它将指针的数据类型从 void* 类型更改为指针存储的地址的相应数据类型。

#include <iostream>
using namespace std;
int main() {
    void* ptr;
    float f = 6.5f;
  
    // now just assign the float address to void pointer
    ptr = &f;
  
    cout << "The Value of void pointer: ";
    // use the type casting in order to print pointer content
    cout << *(static_cast <float*>(ptr));
  
    return 0;
  }

输出

The Value of void pointer: 6.5

程序解释

  • 此程序打印 void 指针 ptr 所指向的地址值。
  • 我们无法使用 *ptr,因为 void 指针不能被解引用。
  • 但是,如果将 void* 指针类型转换为 float* 类型,则可以使用 void 指针指向的值。
  • 在此示例中,使用 static_cast 运算符将指针的数据类型从 void* 更改为 float*。

示例 3:让我们编写一个 C++ 程序,主要使用类型转换来打印 void 指针。


#include 
using namespace std;
int main() {
    void* ptr;//  declaration of the void pointer
    int* ptr1;// declaration of the integer pointer
    int data = 7;//  initialization of the integer variable
  
    ptr = &data; // just store the address of data variable into the void pointer variable
    ptr1 = (int*)ptr; // assigning the void pointer to integer pointer
    
    cout << " Value of *ptr1 will be : " << *ptr1 << endl;
  
    return 0;
  }
 

输出


Value of *ptr1 will be : 7

上述程序的解释和工作原理

  • 在上面所示的程序中,我们分别声明了 void 类型和 int 类型的两个指针变量。
  • 我们还生成了另一个整数类型的变量。
  • 声明后,变量数据的地址保存在 void 指针变量 ptr 中。
  • 为了将 void 指针分配给整数指针,我们必须对 void 指针变量使用强制转换运算符,即 int。
  • void 指针的类型由强制转换运算符指定,它会通知编译器。
  • 对于强制转换,我们必须在括号中输入数据类型和 *,例如 (char) 或 (int)。

需要记住的重要事项

具有 const 或 volatile 限定符的变量的地址不能存储在 void 指针中。

例如


void *ptr;
const double d = 9.0;

// Error occurred:its because of the Invalid conversion from const void* to void*
ptr = &d;
 

C 中的 void 指针和 C++ 中的 void 指针有什么区别?

在 C 中,我们可以将 void 指针分配给任何其他指针类型而无需进行类型转换,但在 C++ 中,将 void 指针分配给任何其他指针类型时必须进行类型转换。

让我们通过一个例子来理解这个概念

C 中的指针


#include <stdio.h>  
int main()  
{  
   void *ptr; // declaration of  void pointer 
   int *ptr1;  // declaration  integer pointer 
   int a =1000;  // initialization  of the integer variable
   ptr=&a; //     storing the address of 'a' in ptr  
   ptr1=ptr; // assigning the void pointer to  integer pointer type.  
   printf("value of *ptr1 will be: %d",*ptr1);  
   return 0;  
}

输出

value of *ptr1 will be: 1000

在上面的程序中,声明了 void 类型和整数类型的两个指针,分别为“ptr”和“ptr1”。此外,我们声明了整数类型变量“a”。声明后,我们将变量“a”的地址提供给指针“ptr”。然后,由于在 C 中将 void 指针分配给任何其他类型的指针时不需要进行类型转换,我们直接将 void 指针分配给整数指针,即 ptr1。

C++ 中的指针


#include <iostream>  
using namespace std;  
int main()  
{  
  void *ptr; //  declaration of void pointer
  int *ptr1; //declaration  of integer pointer 
  int data=50; // initialization   integer variable
  ptr=&data;  // storing the address of data variable into  void pointer variable  
  ptr1=(int *)ptr; // assigning void pointer into  the integer pointer  
  std::cout << "The value of *ptr1 is will be : " <<*ptr1<< std::endl;  
  return 0;  
}  

输出

The value of *ptr1 is will be : 50

在上面的程序中,声明了 void 类型和 int 类型的两个指针变量。我们还创建了一个新的“data”整数类型变量。声明后,变量“data”的地址保存在 void 指针变量“ptr”中。所以现在我们希望将 void 指针分配给整数指针,我们必须对 void 指针变量使用强制转换运算符,由 (int *) 表示。强制转换运算符指定 void 指针的类型,它会通知编译器。要进行强制转换,请在括号中输入数据类型以及 *,例如 (char *) 或 (int *)。