How to Dereference a Void Pointer in C?

Since a pointer holds the memory location of each variable, it is vital to examine this subject. Any data may be used, including arrays, functions, floats, chars, and integers. In addition to describing the distinctions between null Pointer and void Pointer, this article will examine one of the pointer subtypes, void pointer.

A pointer may only be allocated an address that contains the same type of data as the Pointer, as declared upon declaration.

int var = 2;

An integer type variable var is initialized with a value 2;

Also Read: What is Pointer in C Programming?

int ptr = &var;

An address of an integer variable is given to a pointer of type int. This signifies that this pointer ptr cannot point to another variable type.

This issue can be resolved by including a pointer of type void.

What is Void Pointer?

When a pointer isn’t connected to any data type, it becomes a Void Pointer. The address of a variable specifies a specific position in memory.

Also Read: What is Double Pointer in C Language?

How is it used in C?

Syntax: void* pointer_name

Here, a void type pointer may be defined using the above syntax. The name and type of a pointer that supports a certain data type are declared when a pointer is created.

E.g.,

void* ptr;

In this code, a void is the type of Pointer and ptr is the variable name;

What is a Generic-Purpose Pointer?

Pointers to Void serve as all-purpose pointers and may be used to access the contents of any variable. This implies that any address can be associated with any data type and Pointer.

Also Read: What is Dangling Pointer in C Programming Language?

Any address may be inserted and typecast to a void pointer, supporting flexible data type storage. Consequently, a void pointer is another name for a general-purpose pointer.

float *p;

Type float pointer is declared.

void *ptr;

Pointer p of void type is declared.

ptr = &p;

Here, a void pointer ptr holds the address of a float type pointer p.

As was mentioned in Null Pointer, we may dynamically allocate memory using the malloc() or calloc() methods. Because these methods return Void * types, any data type may be used to allocate memory.

NOTE:

int ptr = malloc(sizeof(int));

The malloc function in this code gives the variable ptr a memory address. Although C++ does not permit it, this code can be executed in C. By explicitly typecasting the return value from the malloc method to (int *), this code gets translated into C++.

Why use Void Pointer?

As mentioned before, the data type of a pointer must match the data type declared when the Pointer was defined. We avoid explicit type casting by using Void Pointer.

Multiple variables’ addresses can be stored in a void pointer, and their contents can then be retrieved by using the indirection operator and the appropriate typecasting.

What is the Size of the Void Pointer in C?

The coding environment, however, mostly determines the size of the reference to Void. The Pointer to void size is identical to the Pointer to a character, but only in accordance with the C language standard.

How to Dereference a Void Pointer in C? | Can you Dereference a Void Pointer?

To retrieve the value that a void pointer refers to since void pointers cannot simply be dereferenced, we must utilize the indirection operator (*).

printf(‘%d’,*ptr);

The previous code’s address p is still there in Ptr. The ptr pointer in this situation is being dereferenced.

Since we are writing the value of the variable that is referenced by the pointer “ptr,” a compile-time error will occur for this code.

So we have to re-write the code, as

printf(‘%d’,*(int*)ptr);

Earlier described code typecast the void pointer into an integer pointer. It is then followed by printing the value of the variable it was referring to. The above C/C++ idiom dereferences a void pointer. Hence type cast is essential.

What does a Pointer to a Void cannot Dereference mean?

The compiler needs help to predict how big the pointed object will be since void pointers don’t have a matching data type. The Void * must be typecast before being dereferenced so that the compiler can anticipate the data types.

Can we perform Arithmetic Operations on the Void Pointer?

It is impossible to perform arithmetic operations on void pointers directly in C. A suitable typecasting should be employed to use void pointers for arithmetic operations.

Advantages

  • One may create a generic function by using a void pointer, which takes parameters of any data type.
  • Since we are aware that void pointers might be converted to other data types, any library methods named Malloc, calloc, or realloc return void *.
  • A comprehensive linked list is another option.

Disadvantages/Limitations

  • A void pointer cannot be dereferenced in any way.
  • Pointer arithmetic cannot utilize void pointers according to the C standard.

Applications

  • An exhaustive list of Void Pointers’ potential applications is unnecessary. The qsort function is, nevertheless, the most used.
  • The qsort array sorting function is part of the C standard library. We can sort an array of data containing double, long, and integer values using the qsort function.

Difference between a Void Pointer in C and C++?

In the C programming language, the void pointer can be given to any other pointer type type-free. In C++ programming, typecasting is necessary before assigning a void pointer to any other pointer type.

Difference between Null Pointer and Void Pointer

Null PointerVoid Pointer
  It does not refer to any address.It may direct to any location at any time.
  The assignment operator and the null keyword together produce null pointers.The void keyword is used in place of the data type to produce void pointers.
There is no typecasting involved.Programmers can avoid type converting a pointer variable repeatedly by keeping the pointer type as Void.

Conclusion

Even if the concept of Void is hazy, it is especially crucial to understand it because of how frequently it is used and the conditions surrounding void pointers. Use Void to store several addresses for variables with different data types. Once more, you may define a pointer without needing any memory locations by initializing it with null.

Frequently Asked Questions (FAQs)

1. What does Void *ptr mean?

Ans. A simple declaration of the void pointer variable ptr is what this line of code does.

2. Can a Void Function return a void pointer?

Ans. It’s not necessarily a problem for a void function to return a void value.

3. Can you return a void pointer in C?

Ans. yes. Memory functions that allocate space on demand, such as malloc(), calloc(), and relloc, are excellent examples of functions that return void pointers ().

Greetings, My name is Rumi Sadaf. I work as both a content writer and a programmer. In essence, I explain what I know and aid others in understanding programming concepts. Happy Viewing.

Leave a Comment