char & wchar_t

S

Samant.Trupti

Hi,

There is one thing I am cofused about....
If I have a declareation say

char str[10];
Now if I want to change it to wchar so do I have to change it
like ....

wchar_t str[10]; or double the size? like
wchar_t str[20];

if yes then Why? char and wchar_t wont take care of it?

Thanks
TS
 
I

Ian Collins

Hi,

There is one thing I am cofused about....
If I have a declareation say

char str[10];
Now if I want to change it to wchar so do I have to change it
like ....

wchar_t str[10]; or double the size? like
wchar_t str[20];
The array size is the number of elements of that type. char str[10] is
an array of 10 char, wchar_t str[10] is an array of 10 wchar_t.
 
S

Samant.Trupti

  There is one thing I am cofused about....
If I have a declareation say
   char str[10];
Now if I want to change it to wchar so do I have to change it
like ....
  wchar_t str[10];  or double the size? like
  wchar_t str[20];

The array size is the number of elements of that type.  char str[10] is
an array of 10 char, wchar_t str[10] is an array of 10 wchar_t.

Ok Good this is what I was thinking. Wanted confirm. Thank you.
 
D

dertopper

Hi,

  There is one thing I am cofused about....
If I have a declareation say

   char str[10];
Now if I want to change it to wchar so do I have to change it
like ....

  wchar_t str[10];  or double the size? like
  wchar_t str[20];

if yes then Why? char and wchar_t wont take care of it?

They will. Note that you only specify the number of elements of the
array, not the number of bytes. Thus sizeof(char[10]) == 10, but
sizeof(wchar_t[10]) == 20!
It only gets more troublesome if you are using utf8 characters that
need more than one byte (though doubling the arrays will only lead to
unused space).

Regards,
Stuart
 
S

Samant.Trupti

  There is one thing I am cofused about....
If I have a declareation say
   char str[10];
Now if I want to change it to wchar so do I have to change it
like ....
  wchar_t str[10];  or double the size? like
  wchar_t str[20];
if yes then Why? char and wchar_t wont take care of it?

They will. Note that you only specify the number of elements of the
array, not the number of bytes. Thus sizeof(char[10]) == 10, but
sizeof(wchar_t[10]) == 20!
It only gets more troublesome if you are using utf8 characters that
need more than one byte (though doubling the arrays will only lead to
unused space).

Regards,
Stuart

Well then your suggesion would be double the size?? When I declare
char it is one byte and wchar_t it is 2 bytes, isn't that true?
So when I say char[10] it is 10 bytes so wchar_t[10] should be 20
bytes right??
Thanks
TS
 
G

Greg Herlihy

  There is one thing I am cofused about....
If I have a declareation say
   char str[10];
Now if I want to change it to wchar so do I have to change it
like ....
  wchar_t str[10];  or double the size? like
  wchar_t str[20];
if yes then Why? char and wchar_t wont take care of it?
They will. Note that you only specify the number of elements of the
array, not the number of bytes. Thus sizeof(char[10]) == 10, but
sizeof(wchar_t[10]) == 20!
It only gets more troublesome if you are using utf8 characters that
need more than one byte (though doubling the arrays will only lead to
unused space).

Well then your suggesion would be double the size?? When I declare
char it is one byte and wchar_t it is 2 bytes, isn't that true?
So when I say char[10] it is 10 bytes so wchar_t[10] should be 20
bytes right??

Not necessarily. On some systems (OS X comes to mind) the size of
wchar_t is four bytes. In that case, a wchar_t[10] array requires 40
bytes of storage - or quadruple the amount of memory needed to store a
char[10] array..

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top