onkar onkar - 7 months ago 39
Objective-C Question

assigning to 'uint8_t *' (aka 'unsigned char *' ) from incompatible type 'void *'

uint8_t *bufferPtr=NULL;
size_t bufferPtrSize=0;
bufferPtr=malloc(bufferPtrSize * sizeof(uint8_t))

I have used this same code in File1.m it is working fine. When I am using this on File2.mm it is giving me
assigning to 'uint8_t *' (aka 'unsigned char *' ) from incompatible type 'void *'


This is because .mm files aren't treated as normal Objective-C files but as Objective-C++ files, so the type rules of C++ apply here. In C++, if you decide to have a type (in this case you have a void pointer) and then want another type (in your case a char pointer), then you have to explicitly cast them. The way to do this is static_cast<T>() where T is the target type. Ie: uint8_t *bufferPtr = static_cast<uint8_t *>(malloc(bufferPtrSize * sizeof(uint8_t)));