49529485
49529485

Reputation: 37

How to cast a void pointer to an int?

I have a void* pointer. I want to cast the value at that pointer to an int and add it the value at another void* x. I have already tried:

x += (int)(*pointer);

But this gives me a operand of type 'void' where arithmetic or pointer type is required error

Upvotes: 0

Views: 2033

Answers (1)

Barmar
Barmar

Reputation: 782693

You can't dereference a pointer to void. That would return a value of type void, which doesn't exist.

You have to cast the pointer to an int*, then dereference that.

x += *(int*)pointer;

Upvotes: 7

Related Questions