char pointers

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

    #16
    Re: char pointers

    > >char *name;[color=blue][color=green]
    > >name = "David"
    > >then change the 3rd character to V so the string now reads :[/color]
    >
    > If I remember correctly (somebody correct me if not) C++ still _allows_
    > this as a special case, for backward compatibility with C.[/color]

    "David" is a string literal which in the current C++ standard are of
    type const char[].

    In your code, you declare a pointer to a character and don't
    initialize it. You then point the pointer to a string literal. The
    compiler reserves memory for all string literals, and your code
    shouldn't change this memory, which is what you're trying to do when
    you change the 3rd character. Thus, IMHO, you really should do the
    following. This really should be at least a compiler warning if you
    don't declare it const.

    const char * name = "David";

    If you want a variable, of type char* that you can change, you need to
    do the following.

    char * name = new char[strlen("David") + 1];
    strcpy(name,"Da vid");

    then, you can do this...

    name[2] = 'X';

    and you have to remember to do this at some point

    delete [] name;
    [color=blue]
    > But "David" is a a constant, and you should never try to change a
    > constant.
    >
    > Instead, do this:
    >
    > #include <string>
    >
    > ...
    >
    > std::string name = "David";
    >
    > name[2] = 'D';
    >
    >
    > In this case "David" is copied into a variable, and you can change
    > the contents of variable.[/color]

    Yes, you can do this, however there are times when you may not want to
    use std::string, like in an embedded application. Thus, you need to
    understand how C style string work as well.

    Comment

    • Big Brian

      #17
      Re: char pointers

      > > Hi all,[color=blue][color=green]
      > >
      > > If you have a char pointer (char *var), is there a way I can manipulate
      > > specific characters or blocks of characters within the string?
      > >
      > > eg:
      > >
      > > char *name;
      > > name = "David"
      > > then change the 3rd character to V so the string now reads :
      > >
      > > DaVid?
      > >
      > > Thanks for your help
      > >
      > > Dave[/color]
      >
      > Easy
      >
      > name[2] = 'V';
      >
      > john[/color]

      This code is trying to change the contents of a string literal, which
      is a bad thing. It's not that "easy".

      Comment

      • John Harrison

        #18
        Re: char pointers


        "Big Brian" <work@brianmiel ke.com> wrote in message
        news:d283547.03 09030657.370573 e1@posting.goog le.com...[color=blue][color=green][color=darkred]
        > > >char *name;
        > > >name = "David"
        > > >then change the 3rd character to V so the string now reads :[/color]
        > >
        > > If I remember correctly (somebody correct me if not) C++ still _allows_
        > > this as a special case, for backward compatibility with C.[/color]
        >
        > "David" is a string literal which in the current C++ standard are of
        > type const char[].[/color]

        Actually they are of type char*, otherwise they would not be assignable to a
        char* variable.

        But they are of course non-modifiable in a correct C++ program.

        john


        Comment

        • Andrey Tarasevich

          #19
          Re: char pointers

          John Harrison wrote:[color=blue][color=green][color=darkred]
          >> > >char *name;
          >> > >name = "David"
          >> > >then change the 3rd character to V so the string now reads :
          >> >
          >> > If I remember correctly (somebody correct me if not) C++ still _allows_
          >> > this as a special case, for backward compatibility with C.[/color]
          >>
          >> "David" is a string literal which in the current C++ standard are of
          >> type const char[].[/color]
          >
          > Actually they are of type char*, otherwise they would not be assignable to a
          > char* variable.[/color]

          No, they are of type 'const char[N]' (see 2.13.4/1). The conversion to
          'char*' is provided by a dedicated form of array-to-pointer conversion
          introduced specifically for string literals (see 4.2/2).
          [color=blue]
          > But they are of course non-modifiable in a correct C++ program.[/color]

          --
          Best regards,
          Andrey Tarasevich
          Brainbench C and C++ Programming MVP

          Comment

          • John Harrison

            #20
            Re: char pointers


            "Andrey Tarasevich" <andreytarasevi ch@hotmail.com> wrote in message
            news:vlcb9c9gfn nj2c@news.super news.com...[color=blue]
            > John Harrison wrote:[color=green][color=darkred]
            > >> > >char *name;
            > >> > >name = "David"
            > >> > >then change the 3rd character to V so the string now reads :
            > >> >
            > >> > If I remember correctly (somebody correct me if not) C++ still[/color][/color][/color]
            _allows_[color=blue][color=green][color=darkred]
            > >> > this as a special case, for backward compatibility with C.
            > >>
            > >> "David" is a string literal which in the current C++ standard are of
            > >> type const char[].[/color]
            > >
            > > Actually they are of type char*, otherwise they would not be assignable[/color][/color]
            to a[color=blue][color=green]
            > > char* variable.[/color]
            >
            > No, they are of type 'const char[N]' (see 2.13.4/1). The conversion to
            > 'char*' is provided by a dedicated form of array-to-pointer conversion
            > introduced specifically for string literals (see 4.2/2).
            >[color=green]
            > > But they are of course non-modifiable in a correct C++ program.[/color]
            >[/color]

            Well they say you learn something new every day. Apologies to Brian, but
            being Big I guess he can probably handle it.

            john


            Comment

            • Ron Natalie

              #21
              Re: char pointers


              "John Harrison" <john_andronicu s@hotmail.com> wrote in message news:bj5a45$f3b 78$1@ID-196037.news.uni-[color=blue]
              >
              > Actually they are of type char*, otherwise they would not be assignable to a
              > char* variable.[/color]

              Nope, the standard clearly says in 2.13.4: A string literal has type "array of n const char".

              There is special dispensation in the language that allows an implicit conversion to non-const
              char for compatibility with a quarter century of sloppy C code.


              Comment

              Working...