Void pointer confused me | SoloLearn: Learn to code for FREE!

+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 .

7/9/2018 11:38:51 AM

Muhammad Zubair Irshad

4 Answers

New Answer

+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).

+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.

+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

+1

is it good to donot use void pointer or better way to typecast void pointer