Home - Blog

Void Pointers In C: Why You Need Them in Your Coding Experience

In programming languages, certain concepts make coding so much easier. For example, the void pointer determines the specific data type from an end user’s inputs in the C programming language. Hence, bringing us to discuss void pointers in C. 

Like others, void pointers have relevant functions for their flexibility. But, if you want to save time when coding, your go-to pointer function is a void pointer. This pointer lets you store and retrieve a different object type simultaneously. Further on, you will learn all about the coding techniques involving void pointers in C.

(Image of a programming language)

1. What are Void Pointers in C?

Generally, void pointers are pointers without any related data type. In other words, they are like empty containers. This feature enables them to hold addresses of a type variable of unknown data type. 

C’s void pointer function lets you type-cast them into other data types, enabling easy memory allocation. Subsequently, this allows flexible functions like bytes in memory allocation. Furthermore, void pointers in C help carry out generic functions.

2. How does Void Pointer Work in C?

Overall, the pointer function is essential for programming. At least, we see its importance in addressing management and memory allocation. 

There are two different pointer types; generic pointers and void pointers. Our focus, however, is on void pointers and how they work in C.

To have a void pointer in C, you only need to declare a piece of information with the keyword “void.” 

Overall, referencing and dereferencing play a part in the general-purpose pointer concept. Dereferencing, in this case, is when you obtain a value stored in the pointer variable.

So, if a pointer value is not associated with the available data types, you have type-casting as an option. 

However, note that void pointers do not support any arithmetic operation whatsoever. Instead, we use the indirection operator “*.” For that reason, it becomes necessary to type-cast in the absence of an associated datatype. Typecasting, therefore, relates the data type declared alongside the void pointer. Otherwise, running the program displays a compiler error.

The void size is similar to a character pointer in C. The size object varies depending on the pointer platform. 

These memory management functions are malloc () and calloc (). In return, these functions help allocate memory of the needed data type. In a way, memory allocation uses a format that lets the function pointer enhance other features.

3. Syntax of Void Pointer

Usually, it would be best to use the right syntax to import any programming function. Therefore, the syntax of a void pointer in C is:

Void *pointer_name;

Whereby “void” is the keyword of the type of pointer. So, we have the pointer’s name to which we point and give an address allocation.

Let us take the example below:

int b = 6

char c = ‘z’;

Therefore, in a case where we include a void pointer variable, we have:

Void *p = &b;  //  void pointer allocates and holds int ‘b’ as address of integer.

P = &c; //  void pointer also holds char ‘c’ address.

Going further, let us highlight some syntax examples:

int i=6;         // initialization of integer variable.

int *p;         //  integer pointer declaration.

void *ptr;         //  void pointer declaration.

float *fp;         // floating pointer  declaration.

p=fp;         // invalid pointer.

ptr=p;         // valid pointer.

fp=&i;         // invalid pointer.

ptr=&i;         // valid pointer.

ptr=fp;         // valid pointer.

4. Some Examples of Void Pointers in C With Sample Codes

Moving on, we look at an example of void pointer arguments in the code snippet below:

Sample code #1

In the first of our code samples, we show how to use the malloc () function, thus:

#include <stdio.h>  

#include<malloc.h>  

int main()  

{  

   int a=90;  

   int *x = (int*)malloc(sizeof(int)) ;  

   x=&a;  

   printf(“pointed value by x pointer : %d”,*x);  

    return 0;  

}  

Output

Sample code #2

In this next code, we show the type-casting concept. 

#include <stdio.h>  

int main()  

{  

    void *ptr = NULL; //void pointer variable

    int *p  = NULL;// integer pointer variable

    char *cp = NULL;//character pointer variable 

    float *fp = NULL;//float pointer variable

    //void pointer size

    printf(“void pointer size = %d\n\n”,sizeof(ptr));  

    //integer pointer size

    printf(“integer pointer size = %d\n\n”,sizeof(p));  

    //char pointer size

    printf(“character pointer size = %d\n\n”,sizeof(cp));  

    //float pointer size

    printf(“float pointer size = %d\n\n”,sizeof(fp));  

    return 0;  

}  

Output

Otherwise, see attached video link below on void pointer codes in C programming.

https://youtu.be/-503stjWLhc

5. Uses of void pointer

By far, the void pointer has vast relevance in C language programming.

  • They help point to variables of any data type. 

For better understanding, see the code sample below. Some of them include:

#include<stdio.h>

void main()

{

int num=10;//Initializes a normal variable

void *ptr;//Declares the void pointer

ptr=#//Directs Ptr pointer to num

printf(“The value of integer variable is= %d”,*ptr );

}

However, running this code results in a compile-time error. If you use type-casting on the same code to dereference the pointer, you get the desired output, thus:

#include<stdio.h>

void main()

{

int num=10;//Initializes a normal variable

void *ptr;//Declares the void pointer

ptr=#//Directs Ptr pointer to num

printf(“The integer variable value is %d”, *(int*)ptr );// Dereferencing using typecasting 

}

The integer variable value is 10.

Therefore, by type-casting, we can dereference void pointers.

So, while pointer arithmetic is invalid with void pointers, type-casting helps solve that problem. The output becomes:

(Void pointer make coding easier by revealing the location of data variables)

  • With calloc() and malloc (), the void pointer helps allocate memory location for all data types. 

To illustrate, see the code below:

int main(void) 

    // Note that the malloc() function returns void *  

    // Following up, type-cast to any data type such as char *, int *,… 

    int *x = malloc(sizeof(int) * n); 

Of note, the above program compiles in C only.

  • Last but not least, the void pointer variable helps implement generic functions in C.

Conclusion

The void pointer function is an essential programming tool, in general. Programmers find it useful because there are no data types associated with it. At least, it eliminates the confusion of pointers directing different values and data classes to each other. Hence, void pointers reduce excess type-casting. It supports generic pointer type of data, as well. Therefore, it is also a generic-purpose compiler.

You have a complete guide on working with void pointers in c. However, if you have inquiries or need other programming resources, our contact page is always available.

Avatar photo
Hommer Zhao
Hi, I am Hommer, the founder of WellPCB. So far, we have more than 4,000 customers worldwide. If you have any questions, you can feel free to contact me. I really appreciate any help you can provide.

Services