Size of the void pointer in C:
In C programming, the size of a void pointer is equal to the size of a regular pointer on the system. The size of void pointer can vary based on the system architecture. The size of void pointer is implementation-dependent. the size of a void pointer is equal to the size of a regular pointer on the system. This means that on a 64-bit system, a void pointer would be 8 bytes in size, while on a 32-bit system, a void pointer would be 4 bytes in size.
sizeof operator in C is used to determine the size of a void pointer on a specific system.
size_t size = sizeof(void *);
The above given syntax will give us the size of a void pointer in bytes.
The size of the void pointer in C has the same size as a pointer to a character type. According to C understanding, the representation of a pointer to void is the same as a pointer to a character type. The size of the pointer may vary depending on the platform you are using.
The program for implemetation of the void pointer in C.
b
#include <stdio.h>
int main()
{
void *vd_ptr = NULL; //void pointer declaration
int *ptr = NULL;// integer pointer declaration
char *cptr = NULL;//character pointer
float *fptr = NULL;//float pointer
//show size of void pointer
printf("size occupied by void pointer = %d\n\n",sizeof(vd_ptr));
//show size of integer pointer
printf("size occupied by integer pointer = %d\n\n",sizeof(ptr));
//show size of character pointer
printf("size occupied by character pointer = %d\n\n",sizeof(cptr));
//show size of float pointer
printf("size occupied by float pointer = %d\n\n",sizeof(fptr));
return 0;
}
Output
size occupied by void pointer =8
size occupied by integer pointer =8
size occupied by character pointer =8
size occupied by float pointer =8
Advantages of void pointer in C
In C programming, void pointers offer several advantages:
1. Generic Pointers: Void pointers can be used as generic pointers to store the address of any data type. This flexibility allows for more versatile and dynamic programming, especially in situations where the data type is not known in advance.
2. Memory allocation: Void pointers are commonly used in memory allocation functions such as `malloc` and `calloc` to allocate memory for any data type. This allows dynamic memory allocation without specifying the exact data type at compile time.
3. Function Parameters: Void pointers are often used in function parameters to create functions that can accept arguments of different data types. This makes functions more flexible and reusable in different contexts.
4. Typecasting: Void pointers can be typecast to other pointer types, when necessary, a void pointer can be converted to a specific data type. This typecasting enables manipulation and access of data stored at the memory location pointed to by the void pointer.
The calloc()and malloc() function return the void pointer:
The calloc() and malloc() function return the void pointer, so these functions can be used to allocate the memory dynamically of any data type.
#include <stdio.h>
#include<malloc.h>
int main()
{
int var=40;
int *ptr = (int*)malloc(sizeof(int)) ;
ptr=&var;
printf("Value which is pointed by ptr pointer : %d",*ptr);
return 0;
}
Output
Value which is pointed by ptr pointer :40
Important points related to void pointer are:
1. Void pointers (`void *`) are generic pointers that can point to objects of any data type.
2. Void pointers cannot be directly dereferenced because the compiler has no information about the type or size of the data they point to.
3. To use a void pointer, you must specifically typecast to the appropriate data type before dereferencing.
4. Void pointers are generally used in functions that need to accept different types of data as arguments or return values.
5. The size of the void pointer is implementation dependent and may vary based on system architecture.
6. Void pointers are useful for creating generic data structures and functions that can work with different data types without requiring separate implementations.
Dereferencing a void pointer in C:
In C programming language, dereferencing a void pointer involves first typecasting it to the appropriate data type before accessing the value it represents. Since void pointers are generic and do not have a specific data type associated with them, the compiler must know the data type in order to derefer to the pointer correctly.
Here is an example of how to dereference a void pointer in C:
void *vd_ptr;
int n = 10;
vd_ptr = &n; // Assign the address of 'n' to the void pointer vd_ptr
// Dereference the void pointer vd_ptr by typecasting it to an integer pointer
int *intPtr = (int *)vd_ptr;
printf("Value of number n: %d\n", *intPtr);
In this example, we first assign the address of the integer variable `n` to the void pointer `vd_ptr`. To derefer to a void pointer and access the value of `n`, we typecast the integer pointer `intPtr` before dereferencing the void pointer with `*intPtr`. This allows us to properly access the value stored at the memory location pointed to by the void pointer.
Arithmetic Operation on Void pointer:
In C programming, void pointers cannot be used directly for arithmetic operations because the compiler has no information about the size or type of the data they point to. However, you can perform arithmetic operations on a void pointer after converting or typecasting it into a specific pointer type. Here are some common examples of void pointer arithmetic operations in C programming:
1. Addition:
void *vd_ptr;
int *intPtr;
int y = 20;
intPtr = &y;
vd_ptr = (void *)intPtr;
// Perform addition on the void pointer
int *resPtr = (int *)((char *) vd_ptr + 1);
2. Subtraction:
void *vd_ptr;
double *doublePtr;
double PI= 3.14;
doublePtr = &PI;
vd_ptr = (void *)doublePtr;
// Perform subtraction on the void pointer
double *resPtr = (double *)((char *)vd_ptr - 1);
The examples given above show how you can perform arithmetic operations on void pointers in C programming by typecasting them to a specific pointer type before performing the operations on it.
Example demonstrating the use of a void pointer in C:
#include
void showValue(void *ptr, char type)
{
if (type == 'i')
{
int *intPtr = (int *)ptr;
printf("Integer pointer Value: %d\n", *intPtr);
}
else if (type == 'f')
{
float *floatPtr = (float *)ptr;
printf("Float pointer Value: %f\n", *floatPtr);
}
else if (type == 'c')
{
char *charPtr = (char *)ptr;
printf("Char pointer Value: %c\n", *charPtr);
}
else
{
printf("Unsupported data type\n");
}
}
int main()
{
int intVal = 12;
float floatVal = 3.14;
char charVal = 'J';
void *ptr;
ptr = &intVal;
showValue(ptr, 'i');
ptr = &floatVal;
showValue(ptr, 'f');
ptr = &charVal;
showValue(ptr, 'c');
return 0;
}
In this example given above, the `showValue` function takes a void pointer `ptr` and the character `type` to determine the data type of the value pointed to by the void pointer. The void pointer is then typecast to the appropriate data type (int, float or char) based on the given type and the value is printed accordingly. The 'main' function shows how void pointers can be used to point to different data types and passed to the 'showValue' function for printing.