
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Void in C and C++
Both languages (C and C++) support void pointers, but their behaviour is different. In C, a void pointer can be directly assigned to any other pointer type without the need for a typecast. However, in C++, assigning a void pointer to any other pointe type require an explicit typecast.
In this article, we will learn the differences between void pointers in C and C++..
Void Pointer in C
A void pointer (also called a generic pointer) in C is a special type of pointer that can point to any data type, but doesn't have any type by itself. It can hold the address of any variable (int, float, char, etc.).
Following is the syntax
void *p;
This will work fine in C. Now if we use malloc() to allocate some memory spaces, we can use the explicit typecast, but if we do not do that, it will be also fine. The malloc() function returns void pointer.
int *int_ptr = p; int *int_ptr = malloc(sizeof(int) * 10);
Here the returned void pointer is implicitly converted to integer type pointer.
Example
The following program demonstrates how a void* pointer can point to any type (like int), and how memory is allocated, used, and freed dynamically using malloc and free in C:
#include<stdio.h> #include<stdlib.h> int main() { void *p; int x = 10; p = &x; // Implicit cast from void* to int* (valid in C) int *int_ptr = p; printf("Value: %d\n", *int_ptr); // malloc returns void*, allowed without cast in C int *arr = malloc(5 * sizeof(int)); arr[0] = 100; printf("Array First Value: %d\n", arr[0]); free(arr); return 0; }
Following is the output to the above program:
Value: 10 Array First Value: 100
Void Pointer in C++
In C++, a void pointer is a pointer that can store the address of any data type, but it doesn't know which data type the pointer is pointing to.
Here is the syntax to create void pointer in C:
void* ptr_name;
Now if we want to run the same program in C to C++, we should explicitly typecast the pointers.
void *p; int *int_ptr = (int *) p; int *arr_ptr = (int *) malloc(sizeof(int) * 10);
Example
This program demonstrates how to use void* pointers and dynamic memory in C++ by casting void* to the correct pointer type:
#include<iostream> #include<cstdlib> // For malloc and free int main() { void *p; int x = 10; p = &x; // Error: Cannot convert void* to int* without cast // int *int_ptr = p; // Fix with explicit cast int *int_ptr = (int *)p; std::cout<<"Value: "<<*int_ptr<<std::endl; // Error without cast: malloc returns void* // int *arr = malloc(5 * sizeof(int)); // Correct with explicit cast int *arr = (int *)malloc(5 * sizeof(int)); arr[0] = 100; std::cout<<"Array First Value: "<<arr[0]<<std::endl; free(arr); return 0; }
Following is the output to the above program:
Value: 10 Array First Value: 100
C Void Pointers vs C++ Void Pointers
Following is the table to show the difference between Void in C and C++:
Feature(Void) | C | C++ |
---|---|---|
return type | Allowed | Allowed |
Parameters | void = no parameters | void = no parameters |
int func() | Function with unknown args | Function with no args |
void* pointer | Generic pointer, flexible | Generic pointer, but strict |
Pointer arithmetic | Allowed | Not allowed without a cast |