char & wchar_t

Discussion in 'C++' started by Samant.Trupti@gmail.com, Aug 18, 2008.

  1. Guest

    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
     
    , Aug 18, 2008
    #1
    1. Advertising

  2. Ian Collins Guest

    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.
     
    Ian Collins, Aug 18, 2008
    #2
    1. Advertising

  3. Guest

    On Aug 18, 11:33 am, Ian Collins <> wrote:
    > 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.
     
    , Aug 18, 2008
    #3
  4. Guest

    On 18 Aug., 08:13, 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
     
    , Aug 18, 2008
    #4
  5. Guest

    On Aug 18, 12:05 pm, wrote:
    > On 18 Aug., 08:13, 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
     
    , Aug 18, 2008
    #5
  6. Greg Herlihy Guest

    On Aug 18, 6:49 am, ""
    <> wrote:
    > On Aug 18, 12:05 pm, wrote:
    >
    >
    >
    > > On 18 Aug., 08:13, 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
     
    Greg Herlihy, Aug 18, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adrian Cornish
    Replies:
    2
    Views:
    8,245
    Adrian Cornish
    Jul 12, 2003
  2. Bren
    Replies:
    4
    Views:
    4,175
    Peter van Merkerk
    Oct 7, 2003
  3. Mark A. Gibbs
    Replies:
    1
    Views:
    631
    John Carson
    Mar 29, 2005
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,131
    Ian Collins
    May 9, 2006
  5. Replies:
    3
    Views:
    1,154
    James Kanze
    Aug 15, 2008
Loading...

Share This Page