Can math operations be performed on a void pointer?

No. Pointer addition and subtraction are based on advancing the pointer by a number of elements. By definition, if you have a void pointer, you don�t know what it�s pointing to, so you don�t know the size of what it�s pointing to. If you want pointer arithmetic to work on raw addresses, use character pointers.  

Showing Answers 1 - 2 of 2 Answers

Ted

  • Sep 28th, 2011
 

From the above answer don't understand: If you want pointer arithmetic to work on raw addresses, use character pointers.

However the void pointer can be incremented(or decremented) and it will increment by 1.
int *ip1 = NULL;
int *ip2 = NULL;
int diff;
ip1++;
diff=(ip1-ip2); // = 1, Even if it had been char pointers or long, the diff would be one - it advances by one element.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions