Void Pointer in C
×


Void Pointer in C

128

 A Void pointer in C is a special type of pointer that can hold the memory address of any type of data.

A void pointer does not have associated data memory.

It provides flexibility in memory allocation and type handling.

Void pointers in C are used to create flexible and versatile code that can work with various data types.

When we want to access the values pointed to by the void pointers, we need to typecast them.

The diagram represents a void pointer in C.

 A void pointer (void *) is declared without a specific data type, allowing it to point to any type of data.

 Here, the void pointer points to a memory address (in this case, 0x7fff).

 Since it is a void pointer, it does not have a defined data type associated with it, meaning it cannot be directly dereferenced to access the data it points to.

 Void pointers are commonly used in situations where the exact data type is unknown or when dealing with generic data structures, such as linked lists or dynamic memory allocation.

Size of void pointer in C 

In C, the size of a void pointer is implementation-dependent.

The C standard does not specify a specific size for void pointers because they are intended to be generic pointers that can point to objects of any type.

The size of a void pointer is typically the same as the size of a regular pointer on the platform, which is determined by the underlying architecture.

For example: On a 32-bit system, a void pointer and other pointers are usually 4 bytes in size. On a 64-bit system, they are typically 8 bytes.

Uses of void pointer in C 

 Void pointers in C are like flexible tools.

They help in managing different types of data without worrying about what type they are.

 You can use them to store and handle data in a way that's not fixed to a specific type.

 This flexibility makes them useful in many situations, such as creating versatile functions and working with external libraries.

 Just remember, when using void pointers, you need to be cautious with typecasting to avoid errors.

Syntax of Void pointer in C 

void *ptr;
Example:

// Program of void pointer in C
#include<stdio.h>
int main() {
int X = 10;
char Y = 'A';
// Declaring void pointer
void *ptr;
ptr = & X;

printf("Value of integer: %d\n", *(int*)ptr);
ptr = & Y;
printf("Value of character: %c\n", *(char*)ptr);
return 0;
}
Output:

Value of integer: 10
Value of character: A


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments