coolface
coolface

Reputation: 137

How can I access the wchar_t of a wchar_t*?

This is my code:

   wchar_t wbuffer[512];
   wchar_t* wc = (wchar_t*) malloc(buffer_size);
   int buflen = 0;

// ... stuff


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

what is wrong with this?

Upvotes: 1

Views: 2698

Answers (4)

Jacob Relkin
Jacob Relkin

Reputation: 163288

Dereference wc within your loop.

wbuffer[buflen] = *wc;

Upvotes: 1

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361612

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
}

Upvotes: 1

VinS
VinS

Reputation: 194

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

Upvotes: 0

Liviu T.
Liviu T.

Reputation: 23664

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.

Upvotes: 0

Related Questions