Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Void Pointer in C

Pointers in C are the variables that point to the memory location of another variable. In C programming language, there are different types of pointers, each having its own use case. In this article, we will learn about the Void Pointer in C and will discuss some examples to demonstrate the usage of the Void Pointer in C.

What is Void Pointer in C?

The Void Pointer in C is defined as the type of pointer which does not have any associated data type. It can point to any data type like integer, character, structure, and also to other pointers. The usage of the void pointer in C allows a greater deal of flexibility in programming as it allows the functions and data structure in C to be defined to work with any data type. This is why it is also known as a Generic Pointer.

Syntax of the Void Pointer in C

The void Pointer in C is declared using a special keyword “void”, which indicates that it can be used with any data type. Here is the syntax for declaring a void pointer in C Language.

void *ptrName;

This statement declares a void pointer in C named, “ptrName”. Being a void pointer, it can point to any data type.

Working of Void Pointer in C

The void pointer in C works by storing the address of a memory location. As mentioned above, since it has no data type associated with it, it doesn’t know about the data that it is pointing to. So, the void pointer in C cannot be used to directly access the value of the data that it is pointing to.

To use a void pointer in C, it must be cast to a specific data type in C. This typecasting helps the compiler to determine the size and structure of the data that it is pointing to. This allows the void pointer in C to be dereferenced and used.

Let us see an example to learn it in a better way. This example shows how to use a void pointer in C to allocate memory for an integer.

void *ptr;
int *iptr;
ptr = malloc(sizeof(int));
iptr = (int*)ptr;
  • In the above code snippet, we have declared
    • a void pointer named, “ptr”
    • an integer pointer “iptr”.
  • Then we allocated the memory using the malloc() Function and then assigned the address of this memory to the integer pointer named, “iptr”.
  • Then we have typecast the void pointer “ptr” to an integer pointer using the line “iptr = (int*) ptr”.
  • This statement allows the “ptr” to be used to access the value of the integer stored in the allocated memory.

Size of Void Pointer in C

The size of the Void Pointer in C depends on the type of Platform you are using. For 32-Bit Architecture: 4 Bytes
For 64-Bit Architecture: 8 Bytes

Here is the code which is checking the size of void pointer in C.

#include <stdio.h> 

int main()  
{  
    void *ptr;		//void pointer  
    int *iptr;		// integer pointer  
    char *cptr;		//character pointer  
    float *fptr;	//float pointer  
    
    //size of void pointer  
    printf("size of void pointer = %d\n",sizeof(ptr)); 
    
    //size of integer pointer  
    printf("size of integer pointer = %d\n",sizeof(iptr)); 
    
    //size of character pointer  
    printf("size of character pointer = %d\n",sizeof(cptr)); 
    
    //size of float pointer  
    printf("size of float pointer = %d\n",sizeof(fptr));  
    
    return 0;  
}

Output

size of void pointer = 8
size of integer pointer = 8
size of character pointer = 8
size of float pointer = 8

Here, we have printed the size of the void pointer, which comes out to be 8 bytes as we are using a 64-bit computer. Also, we have printed the size of the other types of pointers to demonstrate that size of each type of pointer is almost the same as all of them are storing the addresses of memory.

Examples of Void Pointer in C

These examples will help in clear understanding of the topic i.e., Void Pointer in C.

Example 1 of Void Pointer in C
This example shows that void pointer in C can store objects of any type and we can access the object of any type with the help of the proper typecasting.

Code

#include<stdio.h>   

int main()   
{   
    int a = 56; 
    float b = 4.5; 
    char c = 'k';   
    
    void *ptr; // declaration of void pointer.  
   
    // assigning the address of variable 'a'.  
    ptr=&a;  
    printf("value of 'a' is : %d",*((int*)ptr));  
   
    // assigning the address of variable 'b'.  
    ptr=&b;  
    printf("\nvalue of 'b' is : %f",*((float*)ptr));  
    
    // assigning the address of variable 'c'.  
    ptr=&c;  
    printf("\nvalue of 'c' is : %c",*((char*)ptr));  
    return 0;  
}

Output

value of 'a' is : 56
value of 'b' is : 4.500000
value of 'c' is : k

Explanation
In this example, we have declared a void pointer to access the value of different types of variables. First, it is used to access an integer by typecasting it into an int pointer. Similarly, it is used for accessing the double and character values.

Example 2 of Void Pointer in C
This example will demonstrate to you that the void pointer in C can be used to implement generic functions in C.

Code

#include <stdio.h>

void swap(void *ptr1, void *ptr2, size_t size) {
    void *temp = malloc(size);
    memcpy(temp, ptr1, size);
    memcpy(ptr1, ptr2, size);
    memcpy(ptr2, temp, size);
    free(temp);
}

int main() {
    int x = 5, y = 10;
    double a = 1.5, b = 2.5;
    char c1 = 'a', c2 = 'b';
    
    printf("Before swap: x = %d, y = %d\n", x, y);
    swap(&x, &y, sizeof(int));
    printf("After swap: x = %d, y = %d\n", x, y);
    
    printf("Before swap: a = %lf, b = %lf\n", a, b);
    swap(&a, &b, sizeof(double));
    printf("After swap: a = %lf, b = %lf\n", a, b);
    
    printf("Before swap: c1 = %c, c2 = %c\n", c1, c2);
    swap(&c1, &c2, sizeof(char));
    printf("After swap: c1 = %c, c2 = %c\n", c1, c2);
    
    return 0;
}

Output

Before swap: x = 5, y = 10
After swap: x = 10, y = 5
Before swap: a = 1.500000, b = 2.500000
After swap: a = 2.500000, b = 1.500000
Before swap: c1 = a, c2 = b
After swap: c1 = b, c2 = a

Explanation
In the above example, the swap() function is implemented using a void pointer. The function accepts two void pointers, ‘ptr1’ and ‘ptr2’, and a size_t variable ‘size’ that specifies the size of the data being swapped. The function allocates memory for a temporary variable ‘temp’, and uses the memcpy() function to copy the data pointed to by ‘ptr1’ to ‘temp’. The function then copies the data pointed to by ‘ptr2’ to ‘ptr1’, and the data pointed to by ‘temp’ to ‘ptr2’. Finally, the function frees the memory allocated for ‘temp’.

In the main() function, the swap() function is called three times, each with different data types. The function works correctly with all three data types and we got the expected output on the console.

Uses of Void Pointer in C

Some common uses of the Void Pointer in C are listed below as.

  • Void Pointer in C is used for implementing the generic function. These functions take a void pointer as an argument and typecast them as per the requirement.
  • Generic Data Structures are implemented using the Void Pointer in C because they can be typecast according to the data type.
  • Void Pointer in C is used for Dynamic Memory Allocation as functions such as calloc and malloc return a void pointer which can be typecast into different types of pointers.

Limitations of Void Pointer in C

The Void Pointer in C faces the following limitations.

  • The Void Pointer in C cannot be dereferenced directly. It must be typecast into other types to access values stored at the memory location.
  • We cannot perform Pointer arithmetic operations on void Pointer in C. To perform operations, they need to be converted into an appropriate Data type.

Conclusion
In conclusion, we can say that the void pointer in C is a powerful tool that helps the programmer in dealing with various data types. The Void Pointer in C is not associated with any data type and can be cast into any other data type as per the requirement. Void Pointer in C helps in implementing the generic functions, generic data structure, and dynamic memory allocation.

Frequently Asked Questions (FAQs)

Some Frequently Asked Questions related to “Void Pointer in C” are given below.

Ques 1. Can you dereference a void pointer?
Ans. No, we cannot deference a void pointer in C as it does not have any data type associated with it. For dereferencing, we need to typecast it into some specific data type.

Ques 2. What happens if you cast a void pointer to the wrong data type?
Ans. If you cast a void pointer to the wrong data type, you may get unexpected results or even cause a program crash. It is important to cast a void pointer to the correct data type before dereferencing it.

Ques 3. Can you perform pointer arithmetic with a void pointer?
Ans. No, we cannot perform the pointer arithmetic with a Void Pointer in C.

Ques 4. What is the size of a void pointer in C?
Ans. The size of the Void Pointer in C depends on the type of platform used for the execution of the program. Generally, the size of a void pointer in C is the same as that of other regular pointers.

Ques 5. Can you use a void pointer to pass multiple arguments to a function in C?
Ans. Yes, we can use the void pointer to pass multiple arguments to a function in C. To do so, we need to pack all the arguments into a struct and then struct to the void pointer.

Leave a Reply

Your email address will not be published. Required fields are marked *