C Programming Tutorial

C Programming Tutorial

Void Pointer in C

Hello Everyone, In this article, we will cover the concept of Void Pointer in C Programming. Void pointers are special types of pointers that can point to any data type, making them very versatile but also requiring careful handling.

We will discuss how to declare, use, and manipulate void pointers, along with some practical examples to help you understand their importance and applications in C programming. Whether you’re new to C or have some experience, this guide will help you to understand basic concet of Void Pointer in C effectively. Let’s get started.

A void pointer is a special type of pointer that can store the address of any data type. It’s like a superhero who can change its powers according to the situation!

Imagine a void pointer is like a mailbox. This mailbox can store letters (data) of different shapes and sizes (data types) without caring about their content. But, to read the letters (access the data), you have to know what’s inside them (know the data type).

Now let’s see an example to make it even more clear:

#include <stdio.h>
int main() {
    int number = 42;
    float decimal = 3.14;

    // Declare void pointers
    void *ptr;

    // Store the address of an int in the void pointer
    ptr = &number;
    printf("The value of number is: %d\\n", *((int *)ptr));

    // Store the address of a float in the void pointer
    ptr = &decimal;
    printf("The value of decimal is: %.2f\\n", *((float *)ptr));

    return 0;
}

Output:

The value of number is: 42
The value of decimal is: 3.14

Now let’s break down the code:

  1. First, we include the stdio.h library to use functions like printf.
  2. Next, we create two variables: number (an integer) and decimal (a float).
  3. We then declare a void pointer called ptr.
  4. We store the address of number in the void pointer ptr. Now, ptr points to the memory location where the number is stored.
  5. To access the value of number, we first cast the void pointer ptr to an integer pointer using (int *)ptr. Then, we use the asterisk to access the value at that memory location. Finally, we print the value using printf.
  6. We do the same thing for the decimal variable. We store its address in the void pointer ptr and cast it to a float pointer using (float *)ptr to access its value. Then, we print the value using printf.
  7. The program ends with a return 0 statement.

Let’s look at another example using a void pointer with a character and a double data type.

#include <stdio.h>
int main() {
    char letter = 'A';
    double largeNumber = 12345.6789;

    // Declare a void pointer
    void *ptr;

    // Store the address of the char in the void pointer
    ptr = &letter;
    printf("The value of letter is: %c\\n", *((char *)ptr));

    // Store the address of the double in the void pointer
    ptr = &largeNumber;
    printf("The value of largeNumber is: %.4lf\\n", *((double *)ptr));

    return 0;
}

Output:

The value of letter is: A
The value of largeNumber is: 12345.6789

Let’s break down the code:

  1. As before, we include the stdio.h library to use functions like printf.
  2. We create two variables: letter (a character) and largeNumber (a double).
  3. We declare a void pointer called ptr.
  4. We store the address of letter in the void pointer ptr. Now, ptr points to the memory location where the letter is stored.
  5. To access the value of letter, we first cast the void pointer ptr to a character pointer using (char *)ptr. Then, we use the asterisk to access the value at that memory location. Finally, we print the value using printf.
  6. We do the same thing for the largeNumber variable. We store its address in the void pointer ptr and cast it to a double pointer using (double *)ptr to access its value. Then, we print the value using printf.
  7. The program ends with a return 0 statement.

This example demonstrates how a void pointer can be used to store the address of different data types, such as a character and a double, just like in the previous example.

Share This Article