Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > char & wchar_t

Reply
Thread Tools

char & wchar_t

 
 
Samant.Trupti@gmail.com
Guest
Posts: n/a
 
      08-18-2008
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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      08-18-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Samant.Trupti@gmail.com
Guest
Posts: n/a
 
      08-18-2008
On Aug 18, 11:33*am, Ian Collins <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > 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.
>
> --
> Ian Collins.


Ok Good this is what I was thinking. Wanted confirm. Thank you.
 
Reply With Quote
 
dertopper@web.de
Guest
Posts: n/a
 
      08-18-2008
On 18 Aug., 08:13, (E-Mail Removed) wrote:
> 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
 
Reply With Quote
 
Samant.Trupti@gmail.com
Guest
Posts: n/a
 
      08-18-2008
On Aug 18, 12:05*pm, (E-Mail Removed) wrote:
> On 18 Aug., 08:13, (E-Mail Removed) wrote:
>
> > 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


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
 
Reply With Quote
 
Greg Herlihy
Guest
Posts: n/a
 
      08-18-2008
On Aug 18, 6:49*am, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> On Aug 18, 12:05*pm, (E-Mail Removed) wrote:
>
>
>
> > On 18 Aug., 08:13, (E-Mail Removed) wrote:
> > > * 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting from windows wchar_t to linux wchar_t yakir22@gmail.com C++ 3 08-15-2008 09:04 AM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
string class that can convert between wchar_t and char Bren C++ 4 10-07-2003 03:24 PM
wchar_t wstring char string transformations Adrian Cornish C++ 2 07-12-2003 08:05 PM



Advertisments