Click here to Skip to main content
15,877,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am created a n unsigned char array like this,

unsigned char* m_buffer[50];

and I'm Initializing like this

m_buffer[50] = (wchar_t*)calloc(AU_DATA_BUF, sizeof(wchar_t));

here I am getting

cannot convert from 'wchar_t *' to 'unsigned char *' error ,
can you please tell me how to typecast it

What I have tried:

unsigned char* m_buffer[50];

m_buffer[50] = (wchar_t*)calloc(AU_DATA_BUF, sizeof(wchar_t));
Updated 1-Mar-19 4:11am

You cannot allocate a memory block with calloc and assign it in that way. The return value of calloc is a pointer so you must store it in a pointer, like this:
unsigned char* m_buffer;

m_buffer = (unsigned char*)calloc(AU_DATA_BUF, sizeof(unsigned char));

// or if you really want wide characters

wchar_t* m_buffer;

m_buffer = (wchar_t*)calloc(AU_DATA_BUF, sizeof(wchar_t));
Share this answer
Richard is correct. In general, in C++ you should use new and delete but even better is to use STL objects. A std::vector<puchar> would likely work for you.

In the rare instances I have use calloc and free I use this little macro :
#define AllocateMemory(count,type)	(type*)calloc(count,sizeof(type))

// example usage to get 50 pointers :

PUCHAR * m_buffer = AllocateMemory( 50, PUCHAR );
No casting is necessary because it is done in the macro.

Do you really want 50 pointers or do you want an array of 50 bytes? It is not clear from your question because it mixes them. The example shows how to get 50 pointers. Using the macro, here is how to get 50 bytes :
UCHAR * m_buffer = AllocateMemory( 50, UCHAR );
Share this answer
Richard MacCutchan 28-Feb-19 11:07am    
Nice macro, but I think your pointer definition is a bit odd.
Rick York 28-Feb-19 11:21am    
I revised the second example to be consistent with the first one. Is that what you meant or is it the PUCHAR?
Richard MacCutchan 28-Feb-19 12:02pm    
No, it's the square brackets, which declare the variable as an array rather than a pointer. It should be:
PUCHAR m_buffer = AllocateMemory( 50, UCHAR );
// or
UCHAR* m_buffer = AllocateMemory( 50, UCHAR );
Rick York 28-Feb-19 12:27pm    
Right - I will change that. Thanks.

That's something I often forget. That notation doesn't work in declarations like that so I usually avoid using it altogether.
Your code has (at least) two problems.
  1. You are trying to assign a block of dynamic (heap) allocated memory to an array (which is, by the way, already allocated on the stack).
  2. You are assigning incompatible types, w_char and unsigned char.

Please note, calloc is not the C++ way for allocating dynamic memory (new it is) and, anyway, new is seldom needed. You might, instead use std::array, e.g.
std::array<unsigned char, 50> ab;
std::array<wchar_t, 30> aw;

for fixed-size memory or, as suggested, std::vector, e.g.
std::vector<unsigned char> vb
std::vector<wchar_t> vw;
for dynamically allocated one.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900