I have a void pointer and want to access elements from it. How could I transform a void * into an unsigned byte pointer so I can access its elements (which I know are actually unsigned bytes). Thanks
Using C++
I have a void pointer and want to access elements from it. How could I transform a void * into an unsigned byte pointer so I can access its elements (which I know are actually unsigned bytes). Thanks
Using C++
You need to cast it to the appropriate type you are using for an "unsigned byte". Once it's casted appropriately, you can access it as if it is an array (access elements).
unsigned char* bytePointer = static_cast<unsigned char*>(originalVoidPointer);
unsigned char elementFive = bytePointer[5];
You typecast. If its is C, then you should use something like:
((unsigned char *) void_ptr)[index];
If its C++, then you shouldn't be using void pointers at all.
// Assuming you have a void* declared:
void* p;
// and it's initialized to point to a valid object (array of bytes)
// somewhere here...
// ...
// You can convert it to a pointer to unsigned char:
unsigned char* bytes = static_cast<unsigned char*>(p);
// ... and access the bytes using []
unsigned char x = bytes[0];
Of course the byte offset you use must be within the size of the object that the void* actually points to.