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;
}
Code Example |
---|
Cpp :: c++ destructor |
Cpp :: C++ Assignment Operators |
Cpp :: trig in c++ |
Cpp :: swap first and last character of string in c++ |
Cpp :: imgui menu bar |
Cpp :: pointer to constant |
Cpp :: string array 2d c++ |
Cpp :: freeing array in c++ |
Cpp :: Dfs program in c++ |
C :: colourful text in c |
C :: how to create random integers from a specific range in c language |
C :: c check if file exists |
C :: convert from integer to string vb |
C :: arma 3 get group size |
C :: reading string with spaces in c |
C :: c print size_t |
C :: random number c |
C :: fonction recursive successeur nombre chaine de caractere en c |
C :: how to read character from a string in c |
C :: c printf uint32_t |
C :: hi servicenow |
C :: how to checkout branch from commit id |
C :: how to scan in c |
C :: c for loop |
C :: how to delete virtual hard disk virtualbox |
C :: isspace |
C :: loading builder in flutter |
C :: c strstr |
C :: link list c |
C :: c break statement |