- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C void Pointer
![]() Share with a Friend |
C Programming - C void Pointer
C void Pointer
A void pointer in C is a special type of pointer that can point to any data type. It is a generic pointer, meaning it can store the address of any variable, regardless of its data type (integer, float, structure, etc.). The void pointer is declared with the void keyword and is often used in situations where the type of the data being pointed to is unknown or can vary.
- Declaration of Void Pointer:
A void pointer is declared using the void keyword followed by the pointer asterisk *.
C
void *ptr;
Here, ptr is a void pointer that can point to any data type.
- Assigning Values to Void Pointer:
A void pointer can be assigned the address of any data type variable. However, since the compiler doesn't know the type of data being pointed to, it cannot directly dereference a void pointer. The compiler requires the type to be specified for dereferencing.
C
#include <stdio.h>
int main() {
int num = 10;
float decimal = 3.14;
// Declaring void pointers
void *ptr;
// Assigning the address of an integer variable
ptr = #
printf("Integer value: %d\n", *(int*)ptr); // Typecast to dereference as an integer
// Assigning the address of a float variable
ptr = &decimal;
printf("Float value: %.2f\n", *(float*)ptr); // Typecast to dereference as a float
return 0;
}
Output:
Integer value: 10
Float value: 3.14
In this example:
- ptr is a void pointer.
- It is first assigned the address of an integer variable num and typecasted to (int*) to dereference it.
- Then, it is assigned the address of a float variable decimal and typecasted to (float*) to dereference it.
- Void Pointer Usage:
- Generic functions: Void pointers are commonly used in generic functions or functions that need to handle different data types. For example, functions that take pointers to data structures or memory locations but are not concerned with the type of the data they are dealing with.
- Memory allocation functions: Functions like malloc() and calloc() return void* because they can allocate memory for any data type.
C
int *arr = (int *)malloc(5 * sizeof(int)); // malloc returns void pointer
- Handling different data types in a single function: When you want to write a function that works with multiple types of data, you can pass a void pointer and use type casting to manipulate the data.
- Typecasting a Void Pointer:
Since the void pointer is not associated with a specific data type, you need to typecast it to the appropriate data type before dereferencing it.
C
#include <stdio.h>
void printData(void *ptr, char type) {
if (type == 'i') {
printf("Integer: %d\n", *(int*)ptr); // Typecast to int
} else if (type == 'f') {
printf("Float: %.2f\n", *(float*)ptr); // Typecast to float
}
}
int main() {
int num = 5;
float decimal = 7.5;
printData(&num, 'i'); // Pass integer
printData(&decimal, 'f'); // Pass float
return 0;
}
Output:
Integer: 5
Float: 7.50
Here, the function printData uses a void pointer to handle different data types (int and float) and uses typecasting to access the correct data.
- Void Pointer Limitations:
- Cannot be dereferenced directly: You cannot dereference a void pointer directly because the compiler doesn't know the type of data it points to. You must first typecast it to the appropriate type.
- Memory allocation: Functions like malloc() and calloc() return void pointers, but when using the allocated memory, you need to cast the void pointer to a specific type.
- Void Pointer with Arrays:
A void pointer can also be used with arrays. Since arrays are just pointers, a void pointer can point to the base address of any array.
C
#include <stdio.h>
void printArray(void *arr, int size, char type) {
if (type == 'i') {
int *intArr = (int*)arr;
for (int i = 0; i < size; i++) {
printf("%d ", intArr[i]);
}
printf("\n");
} else if (type == 'f') {
float *floatArr = (float*)arr;
for (int i = 0; i < size; i++) {
printf("%.2f ", floatArr[i]);
}
printf("\n");
}
}
int main() {
int intArr[] = {1, 2, 3, 4, 5};
float floatArr[] = {3.14, 2.71, 1.41};
printArray(intArr, 5, 'i');
printArray(floatArr, 3, 'f');
return 0;
}
Output:
1 2 3 4 5
3.14 2.71 1.41
In this case, the void pointer is used to point to arrays of different data types (int and float), and typecasting is applied to properly access the array elements.
Key Takeaways:
- A void pointer is a generic pointer type in C that can point to any data type.
- You must typecast a void pointer to the appropriate type before dereferencing it.
- Common uses of void pointers include generic functions, memory allocation, and handling different data types.
- Memory allocation functions like malloc() and calloc() return void pointers.
- Void pointers provide flexibility but require careful management and typecasting to ensure correct data handling.
Using void pointers enhances the flexibility and reusability of functions, especially in cases where you need to handle multiple data types. However, you must exercise caution and ensure proper typecasting to avoid errors.
