void *

void*作为一个“通用类型指针”可以接受任何类型的指针变量

举例:

#include<iostream>
#include<string>
using namespace std;
int main(){
	int * a = new int[5];
	void *b = a;
	//cout<<*b<<endl;//非法的间接寻址
	cout<<"b的地址:"<<b<<endl;
	int *c = (int *)b;
	cout<<"c的地址:"<<c<<endl;
	cout<<"a的地址:"<<a<<endl;
	delete []a;
	system("pause");
	return 0;
}

void *b = a,接收了整型数组地址。但是此时是无法输出*b,会出现“非法的间接寻址

只有将void*强制转为int*才可以使用,在例子这个    int *c = (int *)b; 实现了效果

发布了114 篇原创文章 · 获赞 28 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/ChaoFeiLi/article/details/103603123