
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
What is void pointer in C language?
It is a pointer that can hold the address of any datatype variable (or) can point to any datatype variable.
Declaration
The declaration for void pointer is as follows −
void *pointername;
For example − void *vp;
Accessing − Type cast operator is used for accessing the value of a variable through its pointer.
Syntax
The syntax for void pointer is given below −
* ( (type cast) void pointer)
Example 1
int i=10; void *vp; vp = &i; printf ("%d", * ((int*) vp)); // int * type cast
Example
Following is the C program for void pointer −
#include<stdio.h> main ( ){ int i =10; float f = 5.34; void *vp; vp = &i; printf ("i = %d", * ((int*)vp)); vp = &f; printf ( "f = %f", * ((float*) vp)); }
Output
When the above program is executed, it produces the following result −
i = 10 f = 5.34
Example 2
Given below is the C program for pointer arithmetic in void pointers −
#include<stdio.h> #define MAX 20 int main(){ int array[5] = {12, 19, 25, 34, 46}, i; void *vp = array; for(i = 0; i < 5; i++){ printf("array[%d] = %d
", i, *( (int *)vp + i ) ); } return 0; }
Output
When the above program is executed, it produces the following result −
array[0] = 12 array[1] = 19 array[2] = 25 array[3] = 34 array[4] = 46
- Related Articles
- Differentiate the NULL pointer with Void pointer in C language
- Explain the concept of pointer to pointer and void pointer in C language?
- void pointer in C
- What is the size of void pointer in C/C++?
- Is it safe to delete a void pointer in C/C++?
- How to define pointer to pointer in C language?
- Explain the Union to pointer in C language
- What are different pointer operations and problems with pointers in C language?
- What do you mean by pointer to a constant in C language?
- Explain the concept of pointer accessing in C language
- What is pointer operator & in C++?
- What is Pointer operator * in C++?
- How to access the pointer to structure in C language?
- How to create a pointer for strings using C language?
- Explain the dynamic memory allocation of pointer to structure in C language

Advertisements