Assign a void pointer to an int type pointer

Asked By 40 points N/A Posted on -
qa-featured

Can we assign a void pointer to an int type pointer? If not, why? How can we achieve this?

SHARE
Answered By 0 points N/A #124854

Assign a void pointer to an int type pointer

qa-featured

Yes you can assign a void pointer to an int pointer, in fact to any data type. This means that the function/program is not assigned a memory location yet until it is specifically pointed to a particular data type.

Void pointers
the void type of pointer is a special type of pointer. In C++, void represents the absence of type, so void pointers are pointers that point to a value that has no type (and thus also an undetermined length and undetermined dereference properties).

This allows void pointers to point to any data type, from an integer value or a float to a string of characters. But in exchange they have a great limitation: the data pointed by them cannot be directly dereferenced (which is logical, since we have no type to dereference to), and for that reason we will always have to cast the address in the void pointer to some other pointer type that points to a concrete data type before dereferencing it.

Related Questions