Void pointer confused me | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Void pointer confused me

what is the actual purpose for void pointer if void pointer does not show the value for adress varible then why we use it. because we have no permission to perform any operation on it then why we use it .

9th Jul 2018, 11:38 AM
Muhammad Zubair Irshad
Muhammad Zubair  Irshad - avatar
4 Answers
+ 5
As pointed out by Disvolviĝo; You may wonder why and when you'd want to do that - Basically when you are dealing with generic data, or better compatibility with C code (e.g. malloc returns void pointers).
9th Jul 2018, 12:12 PM
Hatsy Rei
Hatsy Rei - avatar
+ 3
int a = 100; void *ptr = &a; cout << *(int*)ptr << endl; //100 If you cast it, you can use it. It is used when you wanna receive any type variables.
9th Jul 2018, 11:45 AM
Disvolviĝo;
Disvolviĝo; - avatar
+ 3
it used to make generic functions for example sort() is generic in stdlib.h it receives a pointer to function that takes two const void* l and returns int like int compare(const void*,const void*); the same parameters and return type to several functions that compares two variables that we don't know how to compare them or what is the type of them
25th Sep 2019, 8:17 PM
ABADA S
ABADA S - avatar
+ 1
is it good to donot use void pointer or better way to typecast void pointer
9th Jul 2018, 12:16 PM
Muhammad Zubair Irshad
Muhammad Zubair  Irshad - avatar