我创建了一个这样的unsigned char数组, unsigned char * m_buffer [50]; 我正在这样初始化 m_buffer [50] =(wchar_t * )calloc(AU_DATA_BUF,sizeof(wchar_t)); 这里我得到了 无法转换自'wchar_t *'到'unsigned char *'错误, 你能告诉我如何对它进行类型转换 我是什么尝试过: unsigned char * m_buffer [50]; m_buffer [50] =(wchar_t *)calloc(AU_DATA_BUF,sizeof(wchar_t));
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));
推荐答案您无法使用 calloc 并以这种方式分配。 calloc 的返回值是一个指针,因此您必须将其存储在指针中,如下所示: 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));
Richard是正确的。一般来说,在C ++中你应该使用new和delete,但更好的是使用STL对象。一个std :: vector< puchar>可能对你有用。 在极少数情况下,我使用calloc和free我使用这个小宏: 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 );
不需要强制转换,因为它是在宏中完成的。 你真的想要50个指针还是你想要50个数组?字节?你的问题并不清楚,因为它混合了它们。该示例显示了如何获得50个指针。使用宏,这里是如何获得50个字节:
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 );
您的代码(至少)有两个问题。 Your code has (at least) two problems.
请注意, calloc 不是用于分配动态内存的 C ++ 方式( new ),无论如何, new 。您可以使用 std :: array ,例如
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;固定大小的内存,或者如建议的那样, std :: vector ,例如
for fixed-size memory or, as suggested, std::vector, e.g.
std::vector<unsigned char> vb std::vector<wchar_t> vw;用于动态分配的。
更多推荐
错误C2440:'=':无法从'wchar
发布评论