Void Pointer in C

Last Updated :

In C, a void pointer, also known as a generic pointer, is a pointer that can point to any type of data. The void type is a special type that represents the absence of any type. Therefore, a void pointer is a pointer that does not have any type associated with it.

Declaring Void Pointer in C::
Here is an example of how to declare a void pointer in C:

void *ptr;

In the above example, ptr is a void pointer that can point to any type of data.

However, when you use a void pointer, you cannot directly access the value it points to. Before you can access the value, you need to explicitly cast the void pointer to the correct type.

Using Void Pointer in C:
Here is an example of how to use a void pointer:

int i = 10;
float f = 3.14;
void *ptr;

ptr = &i;  // void pointer to the integer variable i
printf("%d\n", *(int *)ptr);  // Output: 10

ptr = &f;  // void pointer to the float variable f
printf("%f\n", *(float *)ptr);  // Output: 3.140000

In the above example, ptr is a void pointer that can point to any type of data. First, we set ptr to point to the integer variable i. To access the value of i using ptr, we first cast ptr to an integer pointer using (int *), and then we dereference it using the * operator. Similarly, we set ptr to point to the float variable f, and then we cast ptr to a float pointer to access its value.

Void pointers are commonly used in C to create generic functions that can operate on any type of data. They can also be used to implement polymorphism in C by creating a collection of void pointers that can point to different types of data.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Comment
Next Article
Mithlesh Upadhyay Published 18 Feb, 2023 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Article Tags :

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read