initialize char string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yang Lee

    initialize char string

    hi
    is this correct

    char teststr[100]="\0";

    or is there another method to initialise;
    thanks
    lee


    --
    Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
  • Christian Kandeler

    #2
    Re: initialize char string

    Yang Lee wrote:
    [color=blue]
    > hi
    > is this correct
    >
    > char teststr[100]="\0";
    >
    > or is there another method to initialise;[/color]

    char teststr[100] = "" suffices, because the string literal already has a
    NUL character at the end (in position 0, in this case).


    Christian

    Comment

    • infobahn

      #3
      Re: initialize char string

      Yang Lee wrote:[color=blue]
      >
      > hi
      > is this correct
      >
      > char teststr[100]="\0";[/color]

      It works.
      [color=blue]
      > or is there another method to initialise;[/color]

      The following both do the same thing, perhaps a touch more elegantly:

      char teststr[100] = "";
      char teststr[100] = {0};

      Comment

      • gooch

        #4
        Re: initialize char string


        infobahn wrote:[color=blue]
        > Yang Lee wrote:[color=green]
        > >
        > > hi
        > > is this correct
        > >
        > > char teststr[100]="\0";[/color]
        >
        > It works.
        >[color=green]
        > > or is there another method to initialise;[/color]
        >
        > The following both do the same thing, perhaps a touch more elegantly:
        >
        > char teststr[100] = "";[/color]
        This initializes the first element to a terminating null character and
        does nothing to the rest of the array.[color=blue]
        > char teststr[100] = {0};[/color]
        This one does not do the same thing. This one initializes the entire
        array to zeros.

        Comment

        • pete

          #5
          Re: initialize char string

          gooch wrote:[color=blue]
          >
          > infobahn wrote:[color=green]
          > > Yang Lee wrote:[color=darkred]
          > > >
          > > > hi
          > > > is this correct
          > > >
          > > > char teststr[100]="\0";[/color]
          > >
          > > It works.
          > >[color=darkred]
          > > > or is there another method to initialise;[/color]
          > >
          > > The following both do the same thing,
          > > perhaps a touch more elegantly:
          > >
          > > char teststr[100] = "";[/color]
          > This initializes the first element to a terminating null character and
          > does nothing to the rest of the array.[color=green]
          > > char teststr[100] = {0};[/color]
          > This one does not do the same thing. This one initializes the entire
          > array to zeros.[/color]

          infobahn is right, gooch is wrong.

          --
          pete

          Comment

          • Michael Mair

            #6
            Re: initialize char string


            pete wrote:[color=blue]
            > gooch wrote:
            >[color=green]
            >>infobahn wrote:
            >>[color=darkred]
            >>>Yang Lee wrote:
            >>>
            >>>>hi
            >>>>is this correct
            >>>>
            >>>>char teststr[100]="\0";
            >>>
            >>>It works.
            >>>
            >>>
            >>>>or is there another method to initialise;
            >>>
            >>>The following both do the same thing,
            >>>perhaps a touch more elegantly:
            >>>
            >>>char teststr[100] = "";[/color]
            >>
            >>This initializes the first element to a terminating null character and
            >>does nothing to the rest of the array.
            >>[color=darkred]
            >>>char teststr[100] = {0};[/color]
            >>
            >>This one does not do the same thing. This one initializes the entire
            >>array to zeros.[/color]
            >
            > infobahn is right, gooch is wrong.[/color]

            To clarify that: C99, 6.7.8#20 (Initializers)
            "If there are fewer initializers in a brace-enclosed list than there are
            elements or members of an aggregate, or fewer characters in a string
            literal used to initialize an array of known size than there are
            elements in the array, the remainder of the aggregate shall be
            initialized implicitly the same as objects that have static storage
            duration."

            That is, the second behaviour gooch was talking about (rest initialised
            to zero) is always what we can expect.


            Cheers
            Michael
            --
            E-Mail: Mine is a gmx dot de address.

            Comment

            • gooch

              #7
              Re: initialize char string

              [color=blue]
              > infobahn is right, gooch is wrong.
              >
              > --
              > pete[/color]

              I stand corrected. I seem to remember reading this in the standard at
              one point but I went to my document after reading your reply and there
              it is just as you say.

              Comment

              • Mabden

                #8
                Re: initialize char string

                "Michael Mair" <Michael.Mair@i nvalid.invalid> wrote in message
                news:36ug98F56o ikrU1@individua l.net...[color=blue]
                >[color=green][color=darkred]
                > >>
                > >>>char teststr[100] = {0};
                > >>
                > >>This one does not do the same thing. This one initializes the entire
                > >>array to zeros.[/color]
                > >
                > > infobahn is right, gooch is wrong.[/color]
                >
                > To clarify that: C99, 6.7.8#20 (Initializers)
                > "If there are fewer initializers in a brace-enclosed list than there[/color]
                are[color=blue]
                > elements or members of an aggregate, or fewer characters in a string
                > literal used to initialize an array of known size than there are
                > elements in the array, the remainder of the aggregate shall be
                > initialized implicitly the same as objects that have static storage
                > duration."
                >
                > That is, the second behaviour gooch was talking about (rest[/color]
                initialised[color=blue]
                > to zero) is always what we can expect.[/color]

                What does this do:

                char teststr[100] = {1,2,3};

                Does teststr now have 1, 2, 3 then 97 zeros? Or 1, 2, and 98 threes? Or
                what?

                --
                Mabden



                Comment

                • kevin.bagust

                  #9
                  Re: initialize char string

                  In article <RZhQd.205$Pz7. 172@newssvr13.n ews.prodigy.com >,
                  Mabden <mabden@sbc_glo bal.net> wrote:[color=blue]
                  > "Michael Mair" <Michael.Mair@i nvalid.invalid> wrote in message
                  > news:36ug98F56o ikrU1@individua l.net...[color=green]
                  > >[/color][/color]
                  [color=blue][color=green]
                  > > That is, the second behaviour gooch was talking about (rest[/color]
                  > initialised[color=green]
                  > > to zero) is always what we can expect.[/color][/color]
                  [color=blue]
                  > What does this do:[/color]
                  [color=blue]
                  > char teststr[100] = {1,2,3};[/color]
                  [color=blue]
                  > Does teststr now have 1, 2, 3 then 97 zeros? Or 1, 2, and 98 threes? Or
                  > what?[/color]

                  Its 1, 2, 3 then 97 zeros. It uses the values provided and then fills the
                  rest of the array will zeros.

                  Kevin

                  Comment

                  • Michael Mair

                    #10
                    Re: initialize char string



                    Mabden wrote:[color=blue]
                    > "Michael Mair" <Michael.Mair@i nvalid.invalid> wrote in message
                    > news:36ug98F56o ikrU1@individua l.net...
                    >[color=green][color=darkred]
                    >>>>>char teststr[100] = {0};
                    >>>>
                    >>>>This one does not do the same thing. This one initializes the entire
                    >>>>array to zeros.
                    >>>
                    >>>infobahn is right, gooch is wrong.[/color]
                    >>
                    >>To clarify that: C99, 6.7.8#20 (Initializers)
                    >>"If there are fewer initializers in a brace-enclosed list than there[/color]
                    >
                    > are
                    >[color=green]
                    >>elements or members of an aggregate, or fewer characters in a string
                    >>literal used to initialize an array of known size than there are
                    >>elements in the array, the remainder of the aggregate shall be
                    >>initialized implicitly the same as objects that have static storage
                    >>duration."
                    >>
                    >>That is, the second behaviour gooch was talking about (rest[/color]
                    >
                    > initialised
                    >[color=green]
                    >>to zero) is always what we can expect.[/color]
                    >
                    >
                    > What does this do:
                    >
                    > char teststr[100] = {1,2,3};
                    >
                    > Does teststr now have 1, 2, 3 then 97 zeros? Or 1, 2, and 98 threes? Or
                    > what?[/color]

                    Think hard, mock elsewhere.
                    Hint: Integer variables of static storage duration without an explicit
                    initialiser are initialised to 0, for arrays this applies elementwise.


                    -Michael
                    --
                    E-Mail: Mine is a gmx dot de address.

                    Comment

                    • Mabden

                      #11
                      Re: initialize char string

                      "Michael Mair" <Michael.Mair@i nvalid.invalid> wrote in message
                      news:37drkgF5bc ipkU1@individua l.net...[color=blue]
                      >
                      >
                      > Mabden wrote:[color=green]
                      > > "Michael Mair" <Michael.Mair@i nvalid.invalid> wrote in message
                      > > news:36ug98F56o ikrU1@individua l.net...
                      > >[color=darkred]
                      > >>>>>char teststr[100] = {0};
                      > >>>>
                      > >>>>This one does not do the same thing. This one initializes the[/color][/color][/color]
                      entire[color=blue][color=green][color=darkred]
                      > >>>>array to zeros.
                      > >>>
                      > >>>infobahn is right, gooch is wrong.
                      > >>
                      > >>To clarify that: C99, 6.7.8#20 (Initializers)
                      > >>"If there are fewer initializers in a brace-enclosed list than there[/color]
                      > >
                      > > are
                      > >[color=darkred]
                      > >>elements or members of an aggregate, or fewer characters in a string
                      > >>literal used to initialize an array of known size than there are
                      > >>elements in the array, the remainder of the aggregate shall be
                      > >>initialized implicitly the same as objects that have static storage
                      > >>duration."
                      > >>
                      > >>That is, the second behaviour gooch was talking about (rest[/color]
                      > >
                      > > initialised
                      > >[color=darkred]
                      > >>to zero) is always what we can expect.[/color]
                      > >
                      > >
                      > > What does this do:
                      > >
                      > > char teststr[100] = {1,2,3};
                      > >
                      > > Does teststr now have 1, 2, 3 then 97 zeros? Or 1, 2, and 98 threes?[/color][/color]
                      Or[color=blue][color=green]
                      > > what?[/color]
                      >
                      > Think hard, mock elsewhere.[/color]

                      I was not mocking, this time.
                      [color=blue]
                      > Hint: Integer variables of static storage duration without an explicit
                      > initialiser are initialised to 0, for arrays this applies elementwise.[/color]

                      I was wondering if the array is initialized with the last value or zero.
                      Actually I meant to add a third option (Does is init with
                      1,2,3,1,2,3,1,2 ,3,1,2,3 for the size of the array?) but I posted too
                      quickly and saved myself some embarrassment.. . ooops!

                      I thought the standard might have meant that it would take whatever
                      partial initializers it was given and propagated them to the entire
                      array. But it seems it actually treats it like an automatic variable and
                      fills with zeros.

                      I live and learn from all your wonderful insights.

                      --
                      Mabden


                      Comment

                      Working...