Do you know how to write codes using c programming language? If you know and much contented with this kind of programming language ,then you will agree with me that there are indeed some things you can’t do using this language unlike to c++ and java. You will agree with me that it is difficult if not possible to perform arithmetic on void pointers in c programming. But why it is no possible to perform arithmetic on void pointers in c programming? Find out by reading this piece of content.
The compiler gets confused when void pointer is used as it has no specified return type. Actually void pointers in c can point to any memory location, this will confuse the compiler to specifying the number of bytes to decrement or increment during the execution of pointer arithmetic on void pointers as it is required that void pointers be first typecast to a specific return type before they can called or pointed in pointer arithmetic. This is explained in the c program implementing pointer in the below source code.
//Let us have variable c and p
Void *p= malloc (size of (char)*10);
And then increment p++
P++;
// the compiler gets confused on how much to increment in reference to pointer for the p++ operation
//Then for char c
Char*= (char*) p;
//Then increment c
C++;
// the compiler will execute this operation by incrementing the c by 1 as the size of the memory is specific based on char which is usually 1
The return type is specific
Final note
Now you have seen why it is no possible to perform arithmetic on void pointers in c programming but keep in mind it is possible only when you specify the return type from which pointer variable will take, nothing less than that will result to syntax error. Learn and enjoy writing codes with the use of c .c is simple and compatible to many platforms. Basically, It is not possible to perform arithmetic operation on void pointers since size of the pointer is not known.