错误C2440:'=':无法从'wchar

编程入门 行业动态 更新时间:2024-10-24 12:23:19
本文介绍了错误C2440:'=':无法从'wchar_t *'转换为'unsigned char *'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了一个这样的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.
  • 您正在尝试分配一个动态(堆)分配的块内存到数组(顺便说一下,已经在堆栈上分配)。
  • 您正在分配不兼容的类型, w_char 和 unsigned char 。
  • 请注意, 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

    本文发布于:2023-10-07 14:35:12,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1469647.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:错误   wchar

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!