tags:

views:

148

answers:

4

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++

+6  A: 

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];
Reed Copsey
What's a `byte` ?
Charles Bailey
There is no type named "byte" in C.
Aviral Dasgupta
Yes, I am surprised at the two upvotes considering there is no such thing as a 'byte' type in C++.
Ed Swangren
Th OP said they were "unsigned bytes". Presumably, (s)he has a `typedef char byte;` defined.
Fred Larson
@Fred: That was my assumption when I wrote this. Clarified my answer.
Reed Copsey
@Charles, @aviraldg, and @Ed: I was assuming, given the OP's wording, that there was a byte typedef in place. If not, he could of course use unsigned char.
Reed Copsey
@Fred Larson: Seems like a strange assumption to me considering that C++ has a native type for unsigned bytes called `unsigned char`. And how does `typedef char byte;` magically create an `unsigned byte` type?
Charles Bailey
If he had cast it to Foo no one would question it. But by using the word byte he's committed some kind of error. Interesting.....
jmucchiello
@jmucchiello: What's Foo? ;)
Charles Bailey
Perhaps I'm missing something painfully obvious but I just can't see any way to make `unsigned byte` actual refer to a type or any way to make the syntax `reinterpret_cast<unsigned byte>(originalVoidPointer)` compile. `typedef char byte;` doesn't magically make `unsigned byte` valid a type and a typedef'ed identifier must be one word so you can't `typedef unsigned char unsigned byte`.
Charles Bailey
OK, you could do `#define byte char` but that's just evil.
Charles Bailey
I've removed my misplaced upvote, both for `unsigned byte` and for that `reinterpret_cast` (`static_cast` would be correct).
avakar
@jmucchiello: Read Charles' answer. Additionally, the reasoning is that Java has the byte type.
Aviral Dasgupta
Reed Copsey
+4  A: 
index[reinterpret_cast<unsigned char*>(voidPointer)]
Dario
Why the cast for C?
Charles Bailey
Because at the time I answered, the question was still tagged C.
Dario
Thanks :-).....
Milo
By the way, I'd avoid that syntax, it's widely known on SO because it's been pointed out several times for general enjoyment, but the average programmer would say that's it's not correct C/C++.
Matteo Italia
-1, why `reinterpret_cast` with implementation defined results instead of a simple and valid `static_cast`?
avakar
No, I really meant why the cast for C. Not that the comment is relevant to the question any more.
Charles Bailey
Without the cast, both in C and in C++, the compiler wouldn't know how to do the pointer arithmetic, since void elements don't have a definite size.
Matteo Italia
@Matteo: You can convert a `void*` to an `unsigned char*` perfectly well without a case and the original form of this answer didn't have as much context as the C++ only version now does, so it wasn't clear how the suggested cast syntax was to be used.
Charles Bailey
That cast in C is done implicitly if you are assigning a void pointer to another pointer type, but in this suggestion there's no assignment, and the compiler, without the cast, wouldn't have any clue that it has to interpret the void pointer as a pointer to unsigned char.
Matteo Italia
@Matteo: As I said, the original form of this answer lacked enough context so it wasn't clear how the suggested cast syntax was to be used.
Charles Bailey
+1  A: 

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.

Aviral Dasgupta
I've run into plenty of external libraries (particularly GUI-related) whose APIs require the use of void pointers. 'twould be a shame to never use them in C++ projects.
If it ain't broken, don't fix it.
Aviral Dasgupta
... implying that "such" libraries should use pointers to the base type instead.
Aviral Dasgupta
void pointers still have plenty of uses in C++, especially when performing low level code.
Brian Neal
I usually write pure C code then, and link against it (with use of extern "C"). But yes, there are *valid* uses of void pointers in C++ code...
Aviral Dasgupta
+6  A: 
// 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.

Charles Bailey
+1 for being very clear and explicit.
Reed Copsey