开发者

How can I access the wchar_t of a wchar_t*?

开发者 https://www.devze.com 2023-01-28 05:05 出处:网络
This is my code: wchar_t wbuffer[512]; wchar_t* wc = (wchar_t*) malloc(buffe开发者_如何学编程r_size);

This is my code:

   wchar_t wbuffer[512];
   wchar_t* wc = (wchar_t*) malloc(buffe开发者_如何学编程r_size);
   int buflen = 0;

// ... stuff


// inside the while loop
   wbuffer[buflen] = (wchar_t)wc;

what is wrong with this?


Dereference wc within your loop.

wbuffer[buflen] = *wc;


First of all, what is buffer_size? Is it multiple of sizeof(wchar_t)? If not, make it!

Second, wc is a pointer to wchar_t, that means, you can access wchar_t of it as wc[index], where maximum value of index can be buffer_size/size(wchar_t) - 1. You code should be something like this:

//this 'if' is needed if you don't have any idea of what buffer_size could be!
if (buffer_size % sizeof(wchar_t))
       buffer_size = (buffer_size / sizeof(wchar_t) + 1) * sizeof(wchar_t);

wchar_t wbuffer[512];
wchar_t* wc = (wchar_t*) malloc(buffer_size);
int buflen = 0;
int maxindex = buffer_size/ sizeof(wchar_t) - 1;
int index = 0;
while ( index <= maxindex)
{
   //use wc[index]; here
    index++; //last line
}


It's unclear what you're trying to do.

  1. wbuffer is uninitialized
  2. you're trying to store the pointer to a wchar_t into an array of wchar_t

Please explain the purpose of the program.


wc variable in your case is a pointer which points to the memory that contains arrays of wide characters. If you want get some wide character via wc variable you should write something like

wbuffer[buflen] = (wchar_t)wc[0];//here you assign first wide character of wc to first wide char of wbuffer, if buflen == 0
0

精彩评论

暂无评论...
验证码 换一张
取 消