Search
 
SCRIPT & CODE EXAMPLE
 

CPP

C++ void pointer

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
Comment

c++ program for void pointers

#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;
}
Comment

void pointer c++

#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;
}

Comment

PREVIOUS NEXT
Code Example
Cpp :: 3 conditions for a while loop c++ 
Cpp :: multiple objects in vector C++ 
Cpp :: deal with bad input cpp 
Cpp :: How to clear keyboard buffer c++ 
Cpp :: stack in c++ data structure 
Cpp :: find a member variable in a vector of objects cpp 
Cpp :: c++ program to use nmap 
Cpp :: distructor of the node of the link list 
Cpp :: how to traverse string like array in cpp 
Cpp :: Jython Java Python 
Cpp :: how to find total numbe of distinct characters in a string in c 
Cpp :: changing key bindings in visual code not working 
Cpp :: choose endianness in cpp 
Cpp :: temporary variable ex c++ 
Cpp :: check if string in vector c++ 
Cpp :: Structure of s void function 
Cpp :: is vowel c++/c 
Cpp :: 1162261467 
Cpp :: c++ compile to msi 
Cpp :: operator overloading 
Cpp :: auto i cpp 
Cpp :: concatenate 2 vectors in c++ 
Cpp :: loop in c++ 
Cpp :: c++ press any key 
Cpp :: c++ influenced 
C :: pi in c language 
C :: dynamic 2d arr in c 
C :: c gettimeofday example 
C :: add border to image android 
C :: printf signed char 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =