Converting Pointers

You can either have typed pointers, or the void* pointer. Typed pointers will access the memory as if it is the specified type (this has interesting consequences when you have inheritance with classes, but that will be left for Chapter 4, Classes. Thus, if you cast a pointer to a different type and dereference it, the memory will be treated as containing the cast type. It rarely makes sense to do this. The void* pointer cannot be dereferenced, so you can never access data through a void* pointer, to access the data you have to cast the pointer.

The whole reason for the void* pointer type is that it can point to anything. In general, void* pointers should only be used when the type does not matter to that function. For example, the C malloc function returns a void* pointer because the function merely allocates memory; it does not care what that memory will be used for.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset