void *ptr; // ptr is declared as Void pointer
char c;
int i;
float f;
ptr = &c; // ptr has address of character data
ptr = &i; // ptr has address of integer data
ptr = &f; // ptr has address of float data
#include<iostream>
using namespace std;
void print(void* ptr, char type) {
switch (type)
{
case'i': {
cout << *((int*)ptr) << endl;
}break;
case'c': {
cout << *((char*)ptr) << endl;
}break;
}
}
int main() {
/*void pointers can hold the
values of other variable like char, int, foat
limitation: Is that u can not
directly dereference a void pointer*/
int num = 5;
char let = 'b';
print(&num, 'i');
print(&let, 'c');
return 0;
}
#include <iostream>
using namespace std;
int main(){
int n = 5;
char letter = 'a';
void* ptr = &n;
void* ptr2 = &letter;
cout << *(int*) ptr << endl;
cout << *(char*) ptr2 << endl;
}