C Foundation

What is C? C Compiler Installation C Extensions C Compiler C Interpreter C Program Structure

C Basics

C Keywords C Data Types C Identifiers C Variables C Constant C Escape Sequences C Constant and Volatile C Typecast

Operators

What is Operator C Comma Operator C Arithmetic Operators C Relational Operators C Logical Operators C Bitwise Operators C Conditional Operators C : : Operator C Operator Priority

Basic IO's

Basic IO's C Formatted Functions C Unformatted Functions C Common Functions

Control Statements

What is Control Statement C if Statement C if else Statement C Nested if Statement C Else if Statement C Break Statement C Continue Statement C Switch Statement C Goto Statement

Looping

What is Control Loop C for Loop C Nested for Loop C while Loop C Nested while Loop C do while Loop C Nested do while loop

Functions

What is Function C User Defined Functions C Recursion C Passing Parameters

Scope

Scope C Local Scope C Global Scope

Storage Classes

What is Storage Class C Auto C Extern C Static C Register

Array

What is Array C One Dimensional Array C Two Dimensional Array C Multi Dimensional Array C Arrays Of Strings

String

What is String C String Functions

Pointer

What is Pointer C Pointers Arithmetic C Pointer to Pointer C Pointers and Arrays C Pointers and Strings C Pointer to Functions Void Pointers Null Pointers C Null and Void Pointer

Structure

What is Structure C Struct within Struct C Array within Structure C Pointer to Structure C Structure and Function C Enum C Bitfield Structure C Type def

Union

What is Union

Files

What is File C read a file C write a file C File Handling C Error Handling C Low Level Disk I/O C Other file functions

Memory Allocation

What is Memory Allocation C Malloc() C Calloc() C Free() C Realloc() C Coreleft()

C Reference

All ASCII Code Basic C Questions

C Interview

C Interview Sets All Star Patterns All Number Patterns All Alphabet Patterns All Series Patterns
The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Void Pointer

What Is Void Pointer?

C programming doesn't provide any generic pointer to pointer type. Instead it provides generic pointer type called void pointer. Thus void pointer is most commonly called as general purpose pointer.

Why Void Pointer Alone Generic?

Void pointer is alone generic because void pointer does not have any associated data type with it. Being generic void pointer can store address of a variable irrespective of it's datatype.

Void Pointer Purpose

The purpose of void pointer is awesome, let us consider that we have to declare three pointer variables of three different type say char*, int* and float* if we have no idea about void pointer we definitely going to declare three pointer variable with particular datatypes. Now your code looks like superfluous with datatypes, to overcome this drawbacks void* was introduced. when you declare a pointer variable of type void a conversions to a particular datatypes are applied automatically by the compiler i.e) if the value of a address stored in a void pointer is int datatype then void will convert into int type automatically at the time of compilation. If typecasting is not made, the compiler won't have any idea about what datatype the address in the void pointer points to.

void pointer in c

Syntax - Void Pointer

void-pointer
void *variable_name;

Void Pointer Dereferencing

Dereferencing pointer variable of void type is bit complex than usual pointer variable. Consider the following program which will not compile .

C Program - Void Pointer - Incorrect

void-pointer-incrct.c
#include <stdio.h>
int main()
{
int i = 5;
void *vptr; // void pointer declaration
vptr = &i;
printf("\nValue of iptr = %d ", *vptr);
return 0;
}

C Program - Void Pointer Program - Correct

While Dereferencing a pointer variable of void type typecasting is necessary. Consider the following program which will compile.

void-pointer-crct.c
#include <stdio.h>
int main()
{
int i = 5;
void *vptr; // void pointer declaration
vptr = &i;
printf("\nValue of iptr = %d ", *(int *)vptr);
return 0;
}
  • Value of iptr = 5

Note:

The void pointer "vptr" is initialized by the address of integer variable 'i'. So, the value must be referred using typecast. If you try to access the data without typecasting, the compiler will show an error.

C Program - Void Pointer To String

void-pointer-string.c
#include <stdio.h>
int function(); // function declaration
int main()
{
char *ptr = "void pointer to string";
void *vptr; // void pointer declaration
vptr = &ptr;
printf("%s" , *(char **)vptr);
return 0;
}
  • void pointer to string

Note:

In the above program pointer to pointer typecasting *(char **)vptr is done to retrieve the string stored in a character pointer variable using void pointer.

Interesting Facts About Void Pointer

  • If we assign a address of integer variable to void pointer. Then void pointer will convert into integer pointer by itself at the time of execution because typecasting becomes mandatory when we use void pointer.
  • If we assign a address of character variable to void pointer. Then void pointer will convert into character pointer by itself at the time of execution because typecasting becomes mandatory when we use void pointer.
  • If we assign a address of floating point variable to void pointer. Then void pointer will convert into floating point pointer by itself at the time of execution because typecasting becomes mandatory when we use void pointer.

Void Pointer Is Used

  • Void pointer is highly prefered in dynamic memory allocation using malloc() and calloc().
  • A void pointer is a most convention way in c for storing a raw address.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report

We to update you