I have these defined in the interface of an ObjC class:
unsigned m_howMany;
unsigned char * m_howManyEach;
...
Then later in the code I have this:
m_howManyEach = malloc(sizeof(unsigned) * m_howMany);
which is where I get a warning “Result of malloc is converted to a pointer of type unsigned char, which is incompatible with sizeof operand type unsigned int”
Could someone please explain the proper use of malloc() in this situation, and how to go about getting rid of the warning?
First,
unsignedis reallyunsigned int.The compiler is being nice to you, telling you that you are allocating N items of unsigned, which is not
unsigned char.Also, your later access will be wrong as well.
Change
to
because it looks like you really want
unsigned intas your type instead ofunsigtned char.Of course, this assumes you really want unsigned integers, and not 1-byte unsigned chars.
If the actual size of your integral values is important, you should consider the sized valued (uint8_t, uint16_t, uint32_t, uint64_t).