C Void Pointer

C Void Pointer
Techiio-author
Written by Shuvhojit DebFebruary 6, 2022
11 min read
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this blog, you'll learn about the void pointer of C.

Void Pointer in C

The void pointer in C is a pointer that is not associated with any data types. It points to some data location in the storage means points to the address of variables. It is also called a general-purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.

Syntax of void pointer

void *pointer name;  

The Declaration of the void pointer is given below:

void *ptr;  

In the above declaration, the void is the type of the pointer, and 'ptr' is the name of the pointer.

Algorithm

Begin
   Declare a of the integer datatype.
      Initialize a = 7.
   Declare b of the float datatype.
      Initialize b = 7.6.
   Declare a pointer p as void.
   Initialize p pointer to a.
   Print “Integer variable is”.
      Print the value of a using pointer p.
   Initialize p pointer to b.
   Print “Float variable is”.
      Print the value of b using pointer p
End.

How does Void Pointer Work in C?

The pointer conception in C is veritably useful as it helps in memory allocation and address operation. It helps in enforcing two types of pointers videlicet void pointers and general pointers. Thus, it's occasionally called a general- purpose pointer. Representing and Dereferencing plays a vital part in pointer conception as well as in void pointer.

Let’s walk through the working of the void pointer in C which is performed using pointers that aren't at each associated with any other data type. It contains any data type which will contain the address of the value. a pointer declared with keyword void is a void pointer in C. As mentioned before representing and dereferencing are some of the styles associated with pointer conception which will be used. Dereferencing comes into picture whenever it's a need to pierce the stored value in the pointer variable. Also, there's a type of casting value which is used for dereferencing because none of the pointer value is associated with the data types. The compiler also can not find the type of variable which is refocused by any type of void pointer. One point to keep in mind is void pointer won't support any kind of computation operation. It makes use of indirection driver‘*’to serve the entire purpose. But to serve this problem there's a need to typecast the pointer variable as well for dereferencing. The operation of typecasting is demanded because there's no presence of datatype associated at the time of protestation of the pointer. In short, the compiler does n’t have any applicable source to get an idea of the type of data type declared. So, it performs the typecasting and meets the demand to give an suggestion of the type of data type used by the void pointer at the time of protestation.

Size of the void pointer is the coming point of focus as a void pointer in C functions nearly the same as character pointer in C which means a representation of Character type of pointer will be the same as a void pointer inC. Also, the size will vary according to the platform being used by the pointer. Memory allocation also works in some format which means void pointer has the beauty of furnishing an improvement point of memory operation with calloc () and malloc () functions which eventually returns the void as return type. Thus, these functions can be used to allocate the memory of any data type.

The most important theme line of all the added advantage which pointer has is that it has the power of reusability for void pointers. It can store any type of object and can recoup any type of object from the defined object using the indirection driver and driver with proper typecasting. Dereferencing driver as part of the pointer can be used for easy access or manipulation of the stored data in the memory position for the pointer pointing the data type and it'll have a direct impact on the value of the data type.

Example

Advantage of void pointers:

malloc() and calloc() return void * type and this allows these functions to be used to allocate memory of any data type (just because of void *)

int main(void)
{
// Note that malloc() returns void * which can be
    // typecasted to any type like int *, char *, ..
    int *x = malloc(sizeof(int) * n);
}

Note that the above program compiles in C, but doesn’t compile in C++. In C++, we must explicitly typecast return value of malloc to (int *).

Some Interesting Facts:

1. void pointers cannot be dereferenced. For example the following program doesn’t compile.

int main()
{
    int a = 10;
    void *ptr = &a;
    printf("%d", *ptr);
    return 0;
}

Output:

Compiler Error: 'void*' is not a pointer-to-object type

The following program compiles and runs fine.

int main()
{
    int a = 10;
    void *ptr = &a;
    printf("%d", *(int *)ptr);
    return 0;
}

Output:

10

2. The C standard doesn’t allow pointer arithmetic with void pointers. However, in GNU C it is allowed by considering the size of void is 1. For example the following program compiles and runs fine in gcc.

int main()
{
    int a[2] = {1, 2};
    void *ptr = &a;
    ptr = ptr + sizeof(int);
    printf("%d", *(int *)ptr);
    return 0;
}

Output:

2

Conclusion

void pointer in C is used to mitigate the problem of pointers pointing to each other with a different set of values and data types. There is also a reduction in explicit typecasting. Also, it supports the generic pointer type which makes it as a generic-purpose compiler.

C Language
Void Pointer
Programming
Algorithm
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance