Reset a string?

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

    Reset a string?

    How do i reset a string?
    I just want to empty it som that it does not contain any characters

    Say it contains "hello world" at the time...
    I want it to contain "". Nothing that is..

    Thanx
  • Leor Zolman

    #2
    Re: Reset a string?

    On 5 Mar 2004 12:46:40 -0800, joan@ljungh.se (spike) wrote:
    [color=blue]
    >How do i reset a string?
    >I just want to empty it som that it does not contain any characters
    >
    >Say it contains "hello world" at the time...
    >I want it to contain "". Nothing that is..
    >
    >Thanx[/color]

    Easy: put a '\0' into the first position. Whether "s" is defined as a
    pointer-to-char or an array-of-char, you just say:
    *s = '\0';
    or
    s[0] = '\0';

    But beware that is isn't a pointer-to-const-char; doing this may invoke
    undefined behavior.

    Also note that this doesn't "empty" the string, it just marks the first
    position as the terminating NUL. The memory it was using is still there and
    needs to be contended with.
    -leor




    Leor Zolman
    BD Software
    leor@bdsoft.com
    www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
    C++ users: Download BD Software's free STL Error Message
    Decryptor at www.bdsoft.com/tools/stlfilt.html

    Comment

    • Tor Husabø

      #3
      Re: Reset a string?

      spike wrote:[color=blue]
      > How do i reset a string?
      > I just want to empty it som that it does not contain any characters
      >
      > Say it contains "hello world" at the time...
      > I want it to contain "". Nothing that is..[/color]

      char *str = "hello";
      str[0] = '\0'; /* put a null terminator at the beginning */

      Now the string is considered empty, and strlen(str) will return zero.

      Tor

      Comment

      • Christopher Benson-Manica

        #4
        Re: Reset a string?

        Tor Husabø <torhu@student. hf.uio.no> spoke thus:
        [color=blue]
        > char *str = "hello";
        > str[0] = '\0'; /* put a null terminator at the beginning */[/color]

        As Leor's post notes, this is incorrect code: str points to a string
        literal, and thus may not be modified. You wanted something like

        char str[]="hello";

        --
        Christopher Benson-Manica | I *should* know what I'm talking about - if I
        ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

        Comment

        • Roberto Nunnari

          #5
          Re: Reset a string?

          Tor Husabø wrote:[color=blue]
          > spike wrote:
          >[color=green]
          >> How do i reset a string?
          >> I just want to empty it som that it does not contain any characters
          >>
          >> Say it contains "hello world" at the time...
          >> I want it to contain "". Nothing that is..[/color]
          >
          >
          > char *str = "hello";
          > str[0] = '\0'; /* put a null terminator at the beginning */
          >[/color]

          be careful with char pointers like that.. it's not portable.
          you have no garanties you'll be able to modify it like you did..
          the compiler may put "hello" in readonly memory..

          a correct version should be:

          char str[] = "hello";
          str[0] = '\0';

          Best regards.
          --
          Roberto Nunnari -software engineer-
          mailto:robi@nun nisoft.ch
          Residenza Boschetto 12 tel/fax: +41-91-6046511
          6935 Bosco Luganese """ mobile: +41-76-3208561
          Switzerland (o o)
          =============== =========oOO==( _)==OOo======== =============== =



          -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
          http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
          -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

          Comment

          • Clark Cox

            #6
            Re: Reset a string?

            In article <0O52c.8568$rj4 .114557@news2.e .nsc.no>,
            Tor Husabo <torhu@student. hf.uio.no> wrote:
            [color=blue]
            > spike wrote:[color=green]
            > > How do i reset a string?
            > > I just want to empty it som that it does not contain any characters
            > >
            > > Say it contains "hello world" at the time...
            > > I want it to contain "". Nothing that is..[/color]
            >
            > char *str = "hello";
            > str[0] = '\0'; /* put a null terminator at the beginning */[/color]

            That's undefined behavior, I bealieve that you meant (Notice the change
            in the first line):

            char str[] = "hello";
            str[0] = '\0'; /* put a null terminator at the beginning */

            Comment

            • Eric Sosman

              #7
              Re: Reset a string?

              Roberto Nunnari wrote:[color=blue]
              >
              > Tor Husabø wrote:[color=green]
              > > spike wrote:
              > >[color=darkred]
              > >> How do i reset a string?
              > >> I just want to empty it som that it does not contain any characters
              > >>
              > >> Say it contains "hello world" at the time...
              > >> I want it to contain "". Nothing that is..[/color]
              > >
              > >
              > > char *str = "hello";
              > > str[0] = '\0'; /* put a null terminator at the beginning */
              > >[/color]
              >
              > be careful with char pointers like that.. it's not portable.
              > you have no garanties you'll be able to modify it like you did..
              > the compiler may put "hello" in readonly memory..[/color]

              Even if no read-only memory is involved you can find
              yourself in trouble. For example,

              char *str = "hello";
              str[0] = '\0';
              puts ("Some rhymes: bellow, fellow, Jell-O, and hello");

              may well produce the output

              Some rhymes: bellow, fellow, Jell-O, and

              More than one compiler performs the optimization that
              produces this effect when abused by incorrect code.

              --
              Eric.Sosman@sun .com

              Comment

              • CBFalconer

                #8
                Re: Reset a string?

                Tor Husabø wrote:[color=blue]
                > spike wrote:
                >[color=green]
                >> How do i reset a string? I just want to empty it som that it
                >> does not contain any characters
                >>
                >> Say it contains "hello world" at the time...
                >> I want it to contain "". Nothing that is..[/color]
                >
                > char *str = "hello";
                > str[0] = '\0'; /* put a null terminator at the beginning */
                >
                > Now the string is considered empty, and strlen(str) will return
                > zero.[/color]

                Nope. Now it has undefined (or possibly implementation defined)
                behaviour, which may include going belly up. Think about it for a
                while.

                --
                Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                Available for consulting/temporary embedded and systems.
                <http://cbfalconer.home .att.net> USE worldnet address!


                Comment

                • Jack Klein

                  #9
                  Re: Reset a string?

                  On Fri, 05 Mar 2004 22:12:15 +0100, Roberto Nunnari
                  <robi@nunnisoft .ch> wrote in comp.lang.c:
                  [color=blue]
                  > Tor Husabø wrote:[color=green]
                  > > spike wrote:
                  > >[color=darkred]
                  > >> How do i reset a string?
                  > >> I just want to empty it som that it does not contain any characters
                  > >>
                  > >> Say it contains "hello world" at the time...
                  > >> I want it to contain "". Nothing that is..[/color]
                  > >
                  > >
                  > > char *str = "hello";
                  > > str[0] = '\0'; /* put a null terminator at the beginning */
                  > >[/color]
                  >
                  > be careful with char pointers like that.. it's not portable.
                  > you have no garanties you'll be able to modify it like you did..
                  > the compiler may put "hello" in readonly memory..
                  >
                  > a correct version should be:
                  >
                  > char str[] = "hello";
                  > str[0] = '\0';
                  >
                  > Best regards.[/color]

                  It's more than not portable, it is undefined behavior.

                  The C standard (all versions) does not state that string literals are
                  read-only, or whether or not they may share storage. Attempting to
                  modify string literals in C is undefined behavior because the C
                  standard (all versions) specifically says it is.

                  --
                  Jack Klein
                  Home: http://JK-Technology.Com
                  FAQs for
                  comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
                  comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                  alt.comp.lang.l earn.c-c++

                  Comment

                  • Tor Husabø

                    #10
                    Re: Reset a string?

                    CBFalconer wrote:[color=blue]
                    > Tor Husabø wrote:
                    >[color=green]
                    >>spike wrote:
                    >>
                    >>[color=darkred]
                    >>>How do i reset a string? I just want to empty it som that it
                    >>>does not contain any characters
                    >>>
                    >>>Say it contains "hello world" at the time...
                    >>>I want it to contain "". Nothing that is..[/color]
                    >>
                    >>char *str = "hello";
                    >>str[0] = '\0'; /* put a null terminator at the beginning */
                    >>
                    >>Now the string is considered empty, and strlen(str) will return
                    >>zero.[/color]
                    >
                    >
                    > Nope. Now it has undefined (or possibly implementation defined)
                    > behaviour, which may include going belly up. Think about it for a
                    > while.
                    >[/color]

                    I know, just hasn't written C code for a while.

                    Maybe I should blame the fact that the two declarations below are equal?
                    Maybe it's more confusing than really helpful to differentiate in this way?
                    void func1(int a[]); /* argument is pointer to array */
                    void func2(int *a); /* argument is pointer to a single int */

                    I guess this could be what had me fooled for a moment.

                    Comment

                    • Adrian de los Santos

                      #11
                      Re: Reset a string?

                      On 2004-03-05 15:46:32 -0600, Eric Sosman <Eric.Sosman@su n.com> said:[color=blue]
                      >[color=green]
                      >> be careful with char pointers like that.. it's not portable.
                      >> you have no garanties you'll be able to modify it like you did..
                      >> the compiler may put "hello" in readonly memory..[/color]
                      >
                      > Even if no read-only memory is involved you can find
                      > yourself in trouble. For example,
                      >
                      > char *str = "hello";
                      > str[0] = '\0';
                      > puts ("Some rhymes: bellow, fellow, Jell-O, and hello");
                      >
                      > may well produce the output
                      >
                      > Some rhymes: bellow, fellow, Jell-O, and =
                      >
                      >
                      > More than one compiler performs the optimization that
                      > produces this effect when abused by incorrect code.[/color]


                      Intresting.. very intresting, but why the compiler do this ?

                      Thanks.

                      Comment

                      • Martin Dickopp

                        #12
                        Re: Reset a string?

                        Tor Husabø <torhu@student. hf.uio.no> writes:
                        [color=blue]
                        > Maybe I should blame the fact that the two declarations below are equal?
                        > Maybe it's more confusing than really helpful to differentiate in this way?
                        > void func1(int a[]); /* argument is pointer to array */[/color]

                        No, a pointer to an array cannot be used as an argument here, but a
                        pointer the first element of an array can.
                        [color=blue]
                        > void func2(int *a); /* argument is pointer to a single int */[/color]

                        Both declarations are equivalent. In both cases, `a' is pointer to an
                        object of type `int'. Whether this object is a single `int' or the first
                        element of an array makes no difference as far as the declaration is
                        concerned.

                        Martin


                        --
                        ,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
                        / ,- ) http://www.zero-based.org/ ((_/)o o(\_))
                        \ `-' `-'(. .)`-'
                        `-. Debian, a variant of the GNU operating system. \_/

                        Comment

                        • J. J. Farrell

                          #13
                          Re: Reset a string?


                          "Adrian de los Santos" <demonREMOVE@de mon.com.mx> wrote in message news:c2c4pj$1q1 pnt$3@ID-86516.news.uni-berlin.de...[color=blue]
                          > On 2004-03-05 15:46:32 -0600, Eric Sosman <Eric.Sosman@su n.com> said:[color=green]
                          > >
                          > > Even if no read-only memory is involved you can find
                          > > yourself in trouble. For example,
                          > >
                          > > char *str = "hello";
                          > > str[0] = '\0';
                          > > puts ("Some rhymes: bellow, fellow, Jell-O, and hello");
                          > >
                          > > may well produce the output
                          > >
                          > > Some rhymes: bellow, fellow, Jell-O, and
                          > >
                          > >
                          > > More than one compiler performs the optimization that
                          > > produces this effect when abused by incorrect code.[/color]
                          >
                          > Intresting.. very intresting, but why the compiler do this ?[/color]

                          Because attempting to modify a string literal constant
                          results in undefined behaviour, and the compiler can do
                          what it wants. Since the writer of the code is not allowed
                          to invoke undefined behaviour if he wants predictable
                          results, the compiler can assume that he doesn't do so.
                          Hence the compiler can assume that the string literal
                          "hello" will never be modified, and hence it can optimise
                          memory usage by re-using the string "hello" that occurs
                          at the end of the bigger string. If the coder does risk
                          his life and sanity by modifying the string literal, in
                          this implementation he modifies others at the same time.

                          Comment

                          • CBFalconer

                            #14
                            Re: Reset a string?

                            Tor Husabø wrote:[color=blue][color=green]
                            > > Tor Husabø wrote:[color=darkred]
                            > >> spike wrote:
                            > >>[/color][/color][/color]
                            .... snip ...[color=blue][color=green][color=darkred]
                            > >>
                            > >> char *str = "hello";
                            > >> str[0] = '\0'; /* put a null terminator at the beginning */
                            > >>[/color][/color][/color]
                            .... snip ...[color=blue]
                            >
                            > Maybe I should blame the fact that the two declarations below
                            > are equal? Maybe it's more confusing than really helpful to
                            > differentiate in this way?
                            > void func1(int a[]); /* argument is pointer to array */
                            > void func2(int *a); /* argument is pointer to a single int */
                            >
                            > I guess this could be what had me fooled for a moment.[/color]

                            If the original declaration is of the (correct) form:

                            const char *str = "hello";

                            then the other misusages should get flagged at compile time.

                            --
                            Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                            Available for consulting/temporary embedded and systems.
                            <http://cbfalconer.home .att.net> USE worldnet address!


                            Comment

                            • Adrian de los Santos

                              #15
                              Re: Reset a string?

                              On 2004-03-06 06:14:19 -0600, "J. J. Farrell" <jjf@bcs.org.uk > said:
                              [color=blue]
                              >
                              > "Adrian de los Santos" <demonREMOVE@de mon.com.mx> wrote in message
                              > news:c2c4pj$1q1 pnt$3@ID-86516.news.uni-berlin.de...[color=green]
                              >> On 2004-03-05 15:46:32 -0600, Eric Sosman <Eric.Sosman@su n.com> said:[color=darkred]
                              >>>
                              >>> Even if no read-only memory is involved you can find
                              >>> yourself in trouble. For example,
                              >>>
                              >>> char *str = "hello";
                              >>> str[0] = '\0';
                              >>> puts ("Some rhymes: bellow, fellow, Jell-O, and hello");
                              >>>
                              >>> may well produce the output
                              >>>
                              >>> Some rhymes: bellow, fellow, Jell-O, and
                              >>>
                              >>>
                              >>> More than one compiler performs the optimization that
                              >>> produces this effect when abused by incorrect code.[/color]
                              >>
                              >> Intresting.. very intresting, but why the compiler do this ?[/color]
                              >
                              > Because attempting to modify a string literal constant
                              > results in undefined behaviour[/color]

                              fist pardon me please for my poor knowledge of this topics.

                              As far as i understand

                              char *str="hello";

                              - will create a pointer to a char str
                              - will store "hello" on the memory
                              - will store the address of memory where "hello" starts in the str variable.

                              Am i right ?

                              So why *str becomes a constant ?
                              because it was assigned a rvalue at initialization ? (="hello")

                              Thanks for your kind explanation.

                              Comment

                              Working...