C Programming

What is void* in C and C++

C and C++ are two programming languages that rely heavily on pointers for memory management and data manipulation. Pointers are variables that store memory addresses, which allow programs to access and manipulate data stored in memory. One special type of pointer is the void pointer (void*), also known as a generic pointer.

What is a Void Pointer (void*)?

A void pointer, also known as a generic pointer, is a pointer that is not associated with any specific data type, making it suitable for pointing to any type of data. In other words, a void pointer can point to an integer, a character, a string, or any other data type. This flexibility makes void pointers a powerful tool in C and C++ programming.

Declaration of Void Pointer in C and C++

In C and C++, the void pointer is declared through the following syntax:

void* ptr;

Usage of Void Pointers in C and C++

Void pointers in C are mostly used when the data type of a variable is not known or can vary at runtime. They allow for a generic way to store and manipulate data without needing to know its specific data type. It is helpful especially when dealing with functions or data structures that can handle different types of data.

One common use of void pointers is in memory allocation functions such as malloc() which returns a void pointer point to a block of memory that can be used to store any data type. The programmer can then cast the void pointer to the appropriate data type to access and manipulate the data stored in the allocated memory block.

Another use of void pointers in C is in function pointers, which are variables that store the memory address of a function. Void pointers can be used to store the memory address of any function, regardless of its return type or parameter list, allowing for more flexibility in function pointer usage.

The use of void pointers in C++ is almost similar, however, they can also be used to create a generic interface for objects of different classes. This is often achieved through the use of virtual functions, which allow objects of different classes to be treated uniformly. Further, C++ offers more strict type checking than C, which means that using void pointers incorrectly can result in more errors and bugs.

Points to Consider while Using Void Pointers in C and C++

Here are a few points that you should consider while using void pointers in C and C++.

1: In C, void* can be used as a return value and function parameter but in C++ you must have a specific data type of pointer.

For example:

In C, the code is given below:

#include <stdio.h>

#include <stdlib.h>

void* add_numbers(int a, int b) {

  int* result = malloc(sizeof(int));

  *result = a + b;

  return (void*)result;

}

void print_result(void* result) {

  int* ptr = (int*)result;

  printf("The result is: %d\n", *ptr);

  free(ptr);

}

int main() {

  int num1 = 5, num2 = 7;

  void* result = add_numbers(num1, num2);

  print_result(result);

  return 0;

}

The above code defines a function add_numbers() that returns a void* pointer to the result of adding two integers passed as arguments. The function print_result() takes a void* pointer and prints out the result. In the main() function, we call add_numbers() and pass the resulting void* pointer to print_result() for printing.

In C++, a specific data type is required.

#include <iostream>

using namespace std;

void printInt(int* num) {

cout << "The integer is: " << *num << endl;

}

int main() {

  int x = 10;

  int* ptr = &x;

  printInt(ptr);

  return 0;

}

2: In C, you can convert the void pointer to another pointer type through implicit conversion. But in C++ you have to use the explicit conversion to convert the void pointer to any other pointer type.

Here’s a simple code example for both C and C++ to illustrate the difference between the implicit and explicit conversion of void pointers to other pointer types:

In C, the following code is used:

#include <stdio.h>

void printChar(void* ch) {

char c = *(char*)ch;

printf("%c\n", c);

}

int main() {

  char c = 'a';

  void* ptr = &c;

  printChar(ptr);

  return 0;

}

The above code defines a function printChar that takes a void pointer as a parameter and prints the character stored at that memory location. In the main function, a char variable c is defined and its address is stored in a void pointer ptr. The printChar function is then called with the void pointer “ptr” as an argument. The void pointer is explicitly converted to a char pointer in the “printChar” function to access and print the value stored in “c”.

In C++, the code is given below:

#include <iostream>

void printInt(void* num) {

int* ptr = static_cast<int*>(num);

std::cout << *ptr << std::endl;

}

int main() {

  int x = 10;

  void* ptr = &x;

  printInt(ptr);

  return 0;

}

The above code defines a function printInt that takes a void pointer num as a parameter and casts it to an integer pointer using the static_cast operator. The function then prints the value of the integer pointed to by the casted pointer. In the main function, an integer variable x is defined and its address is stored in a void pointer ptr, which is then passed to the printInt function. The output of the program is the value of x, which is 10.

Final Thoughts

The void pointers in C and C++ provide a powerful way to manipulate data without needing to know its specific data type. They are useful for memory allocation and function pointers, as well as for creating a generic interface for objects of different classes in C++. However, it is important to consider the differences between C and C++ in their usage of void pointers, such as the need for specific data types in C++ and the need for explicit conversion when casting void pointers to other pointer types. Careful consideration of these points can lead to more efficient and effective use of void pointers in programming.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.