Re: const values initializing

Discussion in 'C++' started by ES Kim, Aug 21, 2003.

  1. ES Kim

    ES Kim Guest

    "ralfer" <> wrote in message
    news:bi1vpq$dmc$...
    > Hi,
    > How to create a const char[1000] filled with spaces. Is smarter way than:
    > const char str[1000] = " (...1000 spaces...) ";


    I'd use a const std::string.

    const std::string str(1000, ' ');

    --
    ES Kim
    ES Kim, Aug 21, 2003
    #1
    1. Advertising

  2. ES Kim

    ralfer Guest

    "ES Kim" <> wrote in message
    news:bi1uei$st1$...
    > "ralfer" <> wrote in message
    > news:bi1vpq$dmc$...
    > > Hi,
    > > How to create a const char[1000] filled with spaces. Is smarter way

    than:
    > > const char str[1000] = " (...1000 spaces...) ";

    >
    > I'd use a const std::string.
    >
    > const std::string str(1000, ' ');


    It should be char[1001]. Such expressions sets whole str to zero:
    char str[1001] = {0};

    I was wandering if it's possible initializin const values in similar way.

    regards,
    ralfer

    > --
    > ES Kim
    >
    >
    ralfer, Aug 21, 2003
    #2
    1. Advertising

  3. ES Kim

    Attila Feher Guest

    ralfer wrote:
    [SNIP]
    > It should be char[1001]. Such expressions sets whole str to zero:
    > char str[1001] = {0};
    >
    > I was wandering if it's possible initializin const values in similar
    > way.


    IIRC yes. I do not recall such differences between initializers of const
    and non-const. Only thing is that const must be known compile time.

    --
    Attila aka WW
    Attila Feher, Aug 21, 2003
    #3
  4. ES Kim

    White Wolf Guest

    ralfer wrote:
    [SNIP]
    > Right, there's is no difference between initializing const and
    > non-const. But I'd like to know if it's possible to initialize const
    > with certain (non-zero) value (let's assume 'x').
    >
    > const char str[1000] = {0}; // zeroes whole string
    >
    > const char str[1000] = {'x'}; // only str[0] == 'x'


    To be honest I do not recall the rule (I rarely use such arrays) but IIRC
    the POD is initialized with the given values as long as there are given
    values, then it is zero initialized afterwards.

    --
    WW aka Attila
    White Wolf, Aug 21, 2003
    #4
  5. ES Kim

    ES Kim Guest

    "ralfer" <> wrote in message
    news:bi258u$52g$...
    >
    > "ES Kim" <> wrote in message
    > news:bi1uei$st1$...
    > > "ralfer" <> wrote in message
    > > news:bi1vpq$dmc$...
    > > > Hi,
    > > > How to create a const char[1000] filled with spaces. Is smarter way

    > than:
    > > > const char str[1000] = " (...1000 spaces...) ";

    > >
    > > I'd use a const std::string.
    > >
    > > const std::string str(1000, ' ');

    >
    > It should be char[1001]. Such expressions sets whole str to zero:
    > char str[1001] = {0};


    No, it doesn't. It sets str with 1000 "spaces".
    You know best what fits for your own purpose anyway.

    >
    > I was wandering if it's possible initializin const values in similar way.


    --
    ES Kim
    ES Kim, Aug 21, 2003
    #5
    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. Replies:
    11
    Views:
    1,086
  2. Javier
    Replies:
    2
    Views:
    542
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,089
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    871
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    526
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page