char pointers

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

    char pointers

    Hi all,

    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

  • Alf P. Steinbach

    #2
    Re: char pointers

    On Wed, 03 Sep 2003 03:06:55 +0100, dms <dmsharp@nospam .freeshell.org> wrote:
    [color=blue]
    >If you have a char pointer (char *var), is there a way I can manipulate
    >specific characters or blocks of characters within the string?[/color]

    What has my char pointer to do with your string?


    [color=blue]
    >eg:
    >
    >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.

    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.

    Hth.

    Comment

    • dms

      #3
      Re: char pointers

      Basically, the example was just a basic example for a more complex
      program, but that was the jist of the problem.

      I have multiple classes, all independent of each other, but the only way
      they communicate is through

      char *memory.

      I call functions in each class with &memory as a parameter. How do I
      change individual characters within this array?

      Alf P. Steinbach wrote:[color=blue]
      > On Wed, 03 Sep 2003 03:06:55 +0100, dms <dmsharp@nospam .freeshell.org> wrote:
      >
      >[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?[/color]
      >
      >
      > What has my char pointer to do with your string?
      >
      >
      >
      >[color=green]
      >>eg:
      >>
      >>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.
      >
      > 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.
      >
      > Hth.
      >[/color]

      Comment

      • Alf P. Steinbach

        #4
        Re: char pointers

        On Wed, 03 Sep 2003 03:21:06 +0100, dms <dmsharp@nospam .freeshell.org> wrote:

        [Do not top-post -- next time no answer][color=blue]
        >
        >I have multiple classes, all independent of each other, but the only way
        > they communicate is through
        >
        > char *memory.
        >
        >I call functions in each class with &memory as a parameter.[/color]

        A parameter you're passing around everywhere is indicative of the need
        for a class.

        Instead of passing that parameter around to functions that use it and
        operate on it, let it be the internal state of an object, with member
        functions that use it and operate on it.

        Also, as mentioned in the previous posting, use a std::string if it's
        really a string -- but perhaps there is more to it than that?


        [color=blue]
        >How do I change individual characters within this array?[/color]

        See the previous posting.

        Comment

        • dms

          #5
          Re: char pointers

          I can see your point, and appreciate it... but I like to keep my class
          declerations within their own .h file and it's functions within their
          own .CPP file. The problem with this method is that, if I want to pass a
          variable around in this manner, it has to be passed as a pointer as a
          global variable cannot be used in this case.

          I guess I could make all my current classes subclasses of one giant
          class which encapsulates them all, thus eleviating the problem?

          All my classes at the moment really are independent of each other in
          every respect except for this one variable which needs to be accessed by
          all. I am starting to wish I had put them all in one file with a nice
          global variable at the top... but that's not me.

          Thanks for your help

          Dave

          Alf P. Steinbach wrote:[color=blue]
          > On Wed, 03 Sep 2003 03:21:06 +0100, dms <dmsharp@nospam .freeshell.org> wrote:
          >
          > [Do not top-post -- next time no answer]
          >[color=green]
          >>I have multiple classes, all independent of each other, but the only way
          >> they communicate is through
          >>
          >> char *memory.
          >>
          >>I call functions in each class with &memory as a parameter.[/color]
          >
          >
          > A parameter you're passing around everywhere is indicative of the need
          > for a class.
          >
          > Instead of passing that parameter around to functions that use it and
          > operate on it, let it be the internal state of an object, with member
          > functions that use it and operate on it.
          >
          > Also, as mentioned in the previous posting, use a std::string if it's
          > really a string -- but perhaps there is more to it than that?
          >
          >
          >
          >[color=green]
          >>How do I change individual characters within this array?[/color]
          >
          >
          > See the previous posting.
          >[/color]

          Comment

          • John Harrison

            #6
            Re: char pointers


            "dms" <dmsharp@nospam .freeshell.org> wrote in message
            news:C1c5b.146$ SD5.341733@news fep2-gui.server.ntli .net...[color=blue]
            > Hi all,
            >
            > 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


            Comment

            • Sam Holden

              #7
              Re: char pointers

              On Wed, 3 Sep 2003 05:40:56 +0100,
              John Harrison <john_andronicu s@hotmail.com> wrote:[color=blue]
              >
              > "dms" <dmsharp@nospam .freeshell.org> wrote in message
              > news:C1c5b.146$ SD5.341733@news fep2-gui.server.ntli .net...[color=green]
              >> Hi all,
              >>
              >> 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';[/color]

              If you like crashing programs...

              --
              Sam Holden

              Comment

              • Hafiz Abid Qadeer

                #8
                Re: char pointers

                alfps@start.no (Alf P. Steinbach) wrote in message news:<3f55573d. 801757796@News. CIS.DFN.DE>...[color=blue]
                > On Wed, 03 Sep 2003 03:21:06 +0100, dms <dmsharp@nospam .freeshell.org> wrote:
                >
                > [Do not top-post -- next time no answer][color=green]
                > >
                > >I have multiple classes, all independent of each other, but the only way
                > > they communicate is through
                > >
                > > char *memory.
                > >
                > >I call functions in each class with &memory as a parameter.[/color]
                >
                > A parameter you're passing around everywhere is indicative of the need
                > for a class.
                >
                > Instead of passing that parameter around to functions that use it and
                > operate on it, let it be the internal state of an object, with member
                > functions that use it and operate on it.
                >
                > Also, as mentioned in the previous posting, use a std::string if it's
                > really a string -- but perhaps there is more to it than that?
                >
                >
                >[color=green]
                > >How do I change individual characters within this array?[/color]
                >
                > See the previous posting.[/color]

                it is simple
                memory[index] = x;
                you have to check yourself that index is really not crossing the
                limits of the memory you allocated otherwise buffer overrun will occur.

                Comment

                • John Harrison

                  #9
                  Re: char pointers

                  > >[color=blue][color=green]
                  > > Easy
                  > >
                  > > name[2] = 'V';[/color]
                  >
                  > If you like crashing programs...
                  >[/color]

                  If you want to interpret the OP's code literally, which I obviously didn't
                  do.

                  Now are you going to explain this to the OP or am a I? I understand what you
                  are saying but maybe the OP doesn't.

                  Dear OP, I assumed that you were saying you had a char pointer pointing to a
                  block of chars. My code would modify one of those chars. However it is not
                  permitted in C++ to modify string literals. So if your char pointer is
                  initialised with a string literal then you are not permitted to change that
                  string by any means. I obviously should have made that clear.

                  char* name = "David";
                  name[2] = 'V'; // error

                  char* name = new char[6];
                  strcpy(name, "David");
                  name[2] = 'V'; // OK

                  john


                  Comment

                  • Josephine Schafer

                    #10
                    Re: char pointers


                    "Hafiz Abid Qadeer" <hafizabidqadee r@yahoo.com> wrote in message
                    news:9c67da8b.0 309022057.1a744 e01@posting.goo gle.com...[color=blue]
                    > alfps@start.no (Alf P. Steinbach) wrote in message[/color]
                    news:<3f55573d. 801757796@News. CIS.DFN.DE>...[color=blue][color=green]
                    > > On Wed, 03 Sep 2003 03:21:06 +0100, dms <dmsharp@nospam .freeshell.org>[/color][/color]
                    wrote:[color=blue][color=green]
                    > >
                    > > [Do not top-post -- next time no answer][color=darkred]
                    > > >
                    > > >I have multiple classes, all independent of each other, but the only[/color][/color][/color]
                    way[color=blue][color=green][color=darkred]
                    > > > they communicate is through
                    > > >
                    > > > char *memory.
                    > > >
                    > > >I call functions in each class with &memory as a parameter.[/color]
                    > >
                    > > A parameter you're passing around everywhere is indicative of the need
                    > > for a class.
                    > >
                    > > Instead of passing that parameter around to functions that use it and
                    > > operate on it, let it be the internal state of an object, with member
                    > > functions that use it and operate on it.
                    > >
                    > > Also, as mentioned in the previous posting, use a std::string if it's
                    > > really a string -- but perhaps there is more to it than that?
                    > >
                    > >
                    > >[color=darkred]
                    > > >How do I change individual characters within this array?[/color]
                    > >
                    > > See the previous posting.[/color]
                    >
                    > it is simple
                    > memory[index] = x;
                    > you have to check yourself that index is really not crossing the
                    > limits of the memory you allocated otherwise buffer overrun will occur.[/color]

                    This would lead to an access violation.

                    char *p = "SomeString ";
                    Note that the memory pointed by p is READ only.
                    You cannot write into it.
                    It is a liitle misleading but it should be interpreted as
                    const char *p = "SomeString ";

                    HTH,
                    J.Schafer



                    Comment

                    • Sumit Rajan

                      #11
                      Re: char pointers

                      Dave:

                      I think you may find the following snippet helpful:

                      // david.cpp
                      # include <iostream>

                      void f(char* p)
                      {
                      p[2] = 'V';
                      std::cout << p << '\n';
                      }

                      int main()
                      {
                      char q[] = "David";
                      f(q);

                      char t[] = "David";
                      char* u = t;
                      *(u+2) = 'V';
                      std::cout << u << '\n';
                      }

                      Do tell me whether it answered your question.

                      Regards,
                      Sumit.






                      dms <dmsharp@nospam .freeshell.org> wrote in message news:<C1c5b.146 $SD5.341733@new sfep2-gui.server.ntli .net>...[color=blue]
                      > Hi all,
                      >
                      > 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]

                      Comment

                      • Sam Holden

                        #12
                        Re: char pointers

                        On Wed, 3 Sep 2003 06:01:11 +0100,
                        John Harrison <john_andronicu s@hotmail.com> wrote:[color=blue][color=green][color=darkred]
                        >> >
                        >> > Easy
                        >> >
                        >> > name[2] = 'V';[/color]
                        >>
                        >> If you like crashing programs...
                        >>[/color]
                        >
                        > If you want to interpret the OP's code literally, which I obviously didn't
                        > do.
                        >
                        > Now are you going to explain this to the OP or am a I? I understand what you
                        > are saying but maybe the OP doesn't.[/color]

                        Since you did, I guess you :)

                        --
                        Sam Holden

                        Comment

                        • Hafiz Abid Qadeer

                          #13
                          Re: char pointers

                          "Josephine Schafer" <jsf@usa.net> wrote in message news:<bj3smr$da i1u$1@ID-192448.news.uni-berlin.de>...[color=blue]
                          > "Hafiz Abid Qadeer" <hafizabidqadee r@yahoo.com> wrote in message
                          > news:9c67da8b.0 309022057.1a744 e01@posting.goo gle.com...[color=green]
                          > > alfps@start.no (Alf P. Steinbach) wrote in message[/color]
                          > news:<3f55573d. 801757796@News. CIS.DFN.DE>...[color=green][color=darkred]
                          > > > On Wed, 03 Sep 2003 03:21:06 +0100, dms <dmsharp@nospam .freeshell.org>[/color][/color]
                          > wrote:[color=green][color=darkred]
                          > > >
                          > > > [Do not top-post -- next time no answer]
                          > > > >
                          > > > >I have multiple classes, all independent of each other, but the only[/color][/color]
                          > way[color=green][color=darkred]
                          > > > > they communicate is through
                          > > > >
                          > > > > char *memory.
                          > > > >
                          > > > >I call functions in each class with &memory as a parameter.
                          > > >
                          > > > A parameter you're passing around everywhere is indicative of the need
                          > > > for a class.
                          > > >
                          > > > Instead of passing that parameter around to functions that use it and
                          > > > operate on it, let it be the internal state of an object, with member
                          > > > functions that use it and operate on it.
                          > > >
                          > > > Also, as mentioned in the previous posting, use a std::string if it's
                          > > > really a string -- but perhaps there is more to it than that?
                          > > >
                          > > >
                          > > >
                          > > > >How do I change individual characters within this array?
                          > > >
                          > > > See the previous posting.[/color]
                          > >
                          > > it is simple
                          > > memory[index] = x;
                          > > you have to check yourself that index is really not crossing the
                          > > limits of the memory you allocated otherwise buffer overrun will occur.[/color]
                          >
                          > This would lead to an access violation.
                          >
                          > char *p = "SomeString ";
                          > Note that the memory pointed by p is READ only.
                          > You cannot write into it.
                          > It is a liitle misleading but it should be interpreted as
                          > const char *p = "SomeString ";
                          >
                          > HTH,
                          > J.Schafer[/color]

                          You are right. But I thought that he has allocated memory through malloc
                          or new for this char* and this is not pointing to a constant string.

                          Comment

                          • dms

                            #14
                            Re: char pointers

                            That's great, thanks.


                            I must admit, I didn't know that I was pointing to a constant...

                            Pointers are my biggest week point. I know when I should use them and
                            then just do trial and error until it works as I don't really understand
                            them.

                            Thanks All


                            Dave

                            Sumit Rajan wrote:[color=blue]
                            > Dave:
                            >
                            > I think you may find the following snippet helpful:
                            >
                            > // david.cpp
                            > # include <iostream>
                            >
                            > void f(char* p)
                            > {
                            > p[2] = 'V';
                            > std::cout << p << '\n';
                            > }
                            >
                            > int main()
                            > {
                            > char q[] = "David";
                            > f(q);
                            >
                            > char t[] = "David";
                            > char* u = t;
                            > *(u+2) = 'V';
                            > std::cout << u << '\n';
                            > }
                            >
                            > Do tell me whether it answered your question.
                            >
                            > Regards,
                            > Sumit.
                            >
                            >
                            >
                            >
                            >
                            >
                            > dms <dmsharp@nospam .freeshell.org> wrote in message news:<C1c5b.146 $SD5.341733@new sfep2-gui.server.ntli .net>...
                            >[color=green]
                            >>Hi all,
                            >>
                            >>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][/color]

                            Comment

                            • Thomas Matthews

                              #15
                              Re: char pointers

                              Alf P. Steinbach wrote:
                              [color=blue]
                              > On Wed, 03 Sep 2003 03:06:55 +0100, dms <dmsharp@nospam .freeshell.org> wrote:
                              >
                              >[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?[/color]
                              >
                              >
                              > What has my char pointer to do with your string?
                              >
                              >
                              >
                              >[color=green]
                              >>eg:
                              >>
                              >>[1] char *name;
                              >>[2] 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]

                              In the above example, "David" is a constant string literal.
                              Line 2 assigns a pointer to char to the location of the
                              first character in the constant string literal.

                              Since this is pointer manipulation, it is still valid in
                              C++ as well as C. As a reminder (to newbies), the contents
                              of the string literal _have_not_been_ copied_.

                              Also, because it is a string literal, it cannot be modified
                              without invoking undefined behavior.

                              A safer approach is:
                              char name[] = "David";
                              The above statement creates an array of characters with
                              a capacity of 6 (the number of characters in "David" plus
                              a terminating null), then initializes (i.e. copies) the
                              array locations with the contents of the string literal.

                              The safest approach is to use string as Alf has suggested.

                              --
                              Thomas Matthews

                              C++ newsgroup welcome message:

                              C++ Faq: http://www.parashift.com/c++-faq-lite
                              C Faq: http://www.eskimo.com/~scs/c-faq/top.html
                              alt.comp.lang.l earn.c-c++ faq:

                              Other sites:
                              http://www.josuttis.com -- C++ STL Library book

                              Comment

                              Working...