本教程主要关注 C++ 中的 void 指针,以及如何通过一些示例来使用它们。
在继续本教程之前,请查看 C++ 指针。
语法
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 指针是一种通用指针,当我们在不知道指针所指向变量的数据类型时使用它,正如我们之前在本文中讨论的那样。
#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
程序解释
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
上述程序的解释和工作原理
需要记住的重要事项
具有 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 指针分配给任何其他指针类型时必须进行类型转换。
让我们通过一个例子来理解这个概念
#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。
#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 *)。