what is Void Pointer?

Void pointer or generic pointer is a special type of pointer that can be pointed at objects of any data type. A void pointer is declared like a normal pointer, using the void keyword as the pointer’s type.

Pointers defined using specific data type cannot hold the address of the some other type of variable i.e., it is incorrect in C++ to assign the address of an integer variable to a pointer of type float.

Example:

float *f; //pointer of type float
int i;  //integer variable
f = &i; //compilation error

The above problem can be solved by general purpose pointer called void pointer.

Void pointer can be declared as follows:

void *v // defines a pointer of type void

The pointer defined in this manner do not have any type associated with them and can hold the address of any type of variable.

Example:

void *v; 
int *i;
int ivar;
char chvar;
float fvar;
v = &ivar; // valid
v = &chvar; //valid
v = &fvar; // valid
i = &ivar; //valid
i = &chvar; //invalid
i = &fvar; //invalid
Chitra
Chitra

9 thoughts on “what is Void Pointer?

  1. Good material are produce by ur website. plz give us to better and better materials. thanks a lot

  2. frends jahan tak maine is topic ko samjha hai wo ye hai ki generic pointer is also known as void pointer as we know ki har pointer ka ek data type hota hai but yahan hum koi specific data type ke saath pointer declare nai karte balki void is keyword which use in as data type at the time of generic/void pointer concept…..ye pointer vo hai jiski value yani uska data type kabhi bhi hum typecast karsakte hain by using

    Ex: float *float_pointer;
    int *int_pointer;
    void *void_pointer;
    . . . . . . . .
    . . . . . . . .
    void_pointer = float_pointer;
    . . . . . . . .
    . . . . . . . .
    void_pointer = int_pointer;

    A void pointer is generally used as function parameters, when the parameter or return type is unknown.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in