Open In App
Related Articles

void pointer in C / C++

Improve Article
Improve
Save Article
Save
Like Article
Like

A void pointer is a pointer that has no associated data type with it. A void pointer can hold an address of any type and can be typecasted to any type.

Example of Void Pointer

C




// C Program to demonstrate that a void pointer
// can hold the address of any type-castable type
 
#include <stdio.h>
int main()
{
    int a = 10;
    char b = 'x';
 
    // void pointer holds address of int 'a'
    void* p = &a;
    // void pointer holds address of char 'b'
    p = &b;
}

C++




// C++ Program to demonstrate that a void pointer
// can hold the address of any type-castable type
 
#include <iostream>
using namespace std;
 
int main()
{
    int a = 10;
    char b = 'x';
 
    // void pointer holds address of int 'a'
    void* p = &a;
    // void pointer holds address of char 'b'
    p = &b;
}
 
// This code is contributed by sarajadhav12052009

Time Complexity: O(1)
Auxiliary Space: O(1)

Advantages of Void Pointers

Following are the advantages of void pointers

  • malloc() and calloc() return void * type and this allows these functions to be used to allocate memory of any data type (just because of void *).
  • void pointers in C are used to implement generic functions in C. For example, compare function which is used in qsort().

Some Interesting Facts

1. void pointers cannot be dereferenced.

Example: The following program doesn’t compile.

C




// C Program to demonstrate that a void pointer
// cannot be dereferenced
 
#include <stdio.h>
int main()
{
    int a = 10;
    void* ptr = &a;
    printf("%d", *ptr);
 
    return 0;
}

C++




// C++ Program to demonstrate that a void pointer
// cannot be dereferenced
 
#include <iostream>
using namespace std;
 
int main()
{
    int a = 10;
    void* ptr = &a;
 
    cout << *ptr;
 
    return 0;
}
 
// This code is contributed by sarajadhav12052009

Output

Compiler Error: 'void*' is not a pointer-to-object type

The below program demonstrates the usage of a void pointer to store the address of an integer variable and the void pointer is typecasted to an integer pointer and then dereferenced to access the value. The following program compiles and runs fine.

C




// C program to dereference the void
// pointer to access the value
 
#include <stdio.h>
 
int main()
{
    int a = 10;
    void* ptr = &a;
    // The void pointer 'ptr' is cast to an integer pointer
    // using '(int*)ptr' Then, the value is dereferenced
    // with `*(int*)ptr` to get the value at that memory
    // location
    printf("%d", *(int*)ptr);
    return 0;
}

C++




// CPP program to dereference the void
// pointer to access the value
 
#include <iostream>
using namespace std;
 
int main()
{
    int a = 10;
    void* ptr = &a;
    // The void pointer 'ptr' is cast to an integer pointer
    // using '(int*)ptr' Then, the value is dereferenced
    // with `*(int*)ptr` to get the value at that memory
    // location
    cout << *(int*)ptr << endl;
 
    return 0;
}
 
// This code is contributed by sarajadhav12052009

Output

10

Time Complexity: O(1)
Auxiliary Space: O(1)

2. The C standard doesn’t allow pointer arithmetic with void pointers. However, in GNU C it is allowed by considering the size of the void as 1.

Example: The below C program demonstrates the usage of a void pointer to perform pointer arithmetic and access a specific memory location. The following program compiles and runs fine in gcc.

C




// C program to demonstrate the usage
// of a void pointer to perform pointer
// arithmetic and access a specific memory location
 
#include <stdio.h>
 
int main()
{
    // Declare and initialize an integer array 'a' with two
    // elements
    int a[2] = { 1, 2 };
    // Declare a void pointer and assign the address of
    // array 'a' to it
    void* ptr = &a;
 
    // Increment the pointer by the size of an integer
    ptr = ptr + sizeof(int);
 
    // The void pointer 'ptr' is cast to an integer
    // pointer using '(int*)ptr' Then, the value is
    // dereferenced with `*(int*)ptr` to get the value at
    // that memory location
    printf("%d", *(int*)ptr);
 
    return 0;
}

Output

2

Time Complexity: O(1)
Auxiliary Space: O(1)

Note that the above program may not work in other compilers.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


Last Updated : 11 Jun, 2023
Like Article
Save Article
Similar Reads