copy char*

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

    copy char*


    string a = "abcdef";
    aa[80] = a.c_str();

    bb[40] = how can I get def from aa in here?

    thanks
  • ondra.holub

    #2
    Re: copy char*


    Gary Wessle napsal:
    string a = "abcdef";
    aa[80] = a.c_str();
    >
    bb[40] = how can I get def from aa in here?
    >
    thanks
    a.c_str() + 4

    But you should know, that result of c_str() method is valid only till
    the first change in `a'. So you should make copy of i if you need
    C-string and cannot guarantee, that no change in `a' will occure.

    Comment

    • SirMike

      #3
      Re: copy char*

      Dnia 15 Nov 2006 22:43:39 +1100, Gary Wessle napisa³(a):
      string a = "abcdef";
      aa[80] = a.c_str();
      >
      bb[40] = how can I get def from aa in here?
      use strncpy

      --
      SirMike - http://www.sirmike.org

      C makes it easy to shoot yourself in the foot; C++ makes it harder, but
      when you do, it blows away your whole leg. - Bjarne Stroustrup

      Comment

      • Default User

        #4
        Re: copy char*

        Gary Wessle wrote:
        >
        string a = "abcdef";
        aa[80] = a.c_str();
        >
        bb[40] = how can I get def from aa in here?

        What are aa and bb?



        Brian

        Comment

        • Gary Wessle

          #5
          Re: copy char*

          SirMike <sirmike@FUCKSP AMMERSpoczta.on et.plwrites:
          Dnia 15 Nov 2006 22:43:39 +1100, Gary Wessle napisał(a):
          >
          string a = "abcdef";
          aa[80] = a.c_str();

          bb[40] = how can I get def from aa in here?
          >
          use strncpy
          >
          void func1(const char* p){
          char frst[40];
          char last[40];
          strncpy(frst, p, 3); //first 3 ????
          strncpy(last, p, 3); //last 3 ?????
          }

          string a = "abcdef";
          func1(a.c_str() );

          Comment

          • Gary Wessle

            #6
            Re: copy char*

            I want frst to have "abc" and last to have "def".

            Comment

            • Jim Langston

              #7
              Re: copy char*

              "Gary Wessle" <phddas@yahoo.c omwrote in message
              news:m3odr84k8f .fsf@localhost. localdomain...
              SirMike <sirmike@FUCKSP AMMERSpoczta.on et.plwrites:
              >
              >Dnia 15 Nov 2006 22:43:39 +1100, Gary Wessle napisal(a):
              >>
              string a = "abcdef";
              aa[80] = a.c_str();
              >
              bb[40] = how can I get def from aa in here?
              >>
              >use strncpy
              >>
              >
              void func1(const char* p){
              char frst[40];
              char last[40];
              strncpy(frst, p, 3); //first 3 ????
              strncpy(last, p, 3); //last 3 ?????
              }
              >
              string a = "abcdef";
              func1(a.c_str() );
              void func1( const char* p )
              {
              char first[40];
              char last[40];
              strncpy( first, p, 3 );
              strncpy( last, p + 3, 3 );
              }

              Ugly code. Prefer std::string instead.

              void func1( const std::string Value )
              {
              std::string first = Value.substr( 0, 3 );
              std::string last = Value.substr( 4, 3 );
              }


              Comment

              • BobR

                #8
                Re: copy char*


                Gary Wessle wrote in message ...
                >SirMike <sirmike@FUCKSP AMMERSpoczta.on et.plwrites:
                >
                >use strncpy
                >>
                >
                >void func1(const char* p){
                char frst[40];
                char last[40];
                strncpy(frst, p, 3); //first 3 ????
                strncpy(last, p, 3); //last 3 ?????
                >}
                >
                >string a = "abcdef";
                >func1(a.c_str( ));
                void func1( char const *p, std::ostream &out ){
                char frst[ 40 ];
                char last[ 40 ];
                strncpy( frst, p, 3 ); //first 3 ????
                strncpy( last, p+3, 3 ); //last 3 ?????
                out << "frst="<<frst<< " last="<<last<<s td::endl;
                return;
                }

                {
                std::string a = "abcdef";
                func1( a.c_str(), std::cout );
                }
                // out: frst=abc last=def

                --
                Bob R
                POVrookie


                Comment

                • Default User

                  #9
                  Re: copy char*

                  BobR wrote:
                  void func1( char const *p, std::ostream &out ){
                  char frst[ 40 ];
                  char last[ 40 ];
                  strncpy( frst, p, 3 ); //first 3 ????
                  strncpy( last, p+3, 3 ); //last 3 ?????
                  out << "frst="<<frst<< " last="<<last<<s td::endl;
                  return;
                  }
                  >
                  {
                  std::string a = "abcdef";
                  func1( a.c_str(), std::cout );
                  }
                  // out: frst=abc last=def

                  Did you actually try this? If you got the purported results, then it
                  was by sheer bad luck. You didn't terminate the string, and strncpy
                  doesn't do it for you. Outputting the result was undefined behavior.




                  Brian

                  Comment

                  • Gary Wessle

                    #10
                    Re: copy char*


                    #include <string>
                    #include <iostream>
                    using namespace std;

                    class myType {
                    protected:
                    char _frst[40];
                    char _second[40];
                    char _pair[80];
                    public:
                    const char* frst() const;
                    const char* second() const;
                    myType(const char* pair);
                    };

                    myType::myType( const char* pair)
                    {
                    strncpy(_frst, pair, 3);
                    strncpy(_second , pair+4, 3);
                    }
                    const char* myType::frst()c onst {return _frst;}
                    const char* myType::second( )const {return _second;}

                    int main(){
                    string s = "abc-def";
                    myType mt(s.c_str());
                    string tmp = mt.frst();
                    string tmp2 = mt.second();
                    cout << tmp << " " << tmp2 << endl;
                    }

                    //output
                    abcMh Q%G.ANoN?N =NoN?N=NoN ?N=NoN?N=% @`%G.ANoN? N=%@M$B'\(B $B%G.ANoN? N=(B$B%@(B def

                    Comment

                    • Old Wolf

                      #11
                      Re: copy char*

                      Gary Wessle wrote:
                      string a = "abcdef";
                      aa[80] = a.c_str();
                      This is an error: you can't assign to arrays
                      bb[40] = how can I get def from aa in here?
                      Assuming you have managed to get "a" into "aa" successfully:
                      memcpy(bb, aa + 3, 3);

                      Comment

                      • Gary Wessle

                        #12
                        Re: copy char*

                        I need it to ouput
                        abc def

                        what must be done to
                        strncpy(_frst, pair, 3);
                        strncpy(_second , pair+4, 3);
                        in the c'tor?

                        Comment

                        • BobR

                          #13
                          Re: copy char*


                          Default User wrote in message <4s1h4hFtbh0qU1 @mid.individual .net>...
                          >BobR wrote:
                          >
                          <snip **incomplete** func1>
                          >// out: frst=abc last=def
                          >
                          >Did you actually try this?
                          Yes.
                          If you got the purported results, then it
                          >was by sheer bad luck. You didn't terminate the string, and strncpy
                          >doesn't do it for you. Outputting the result was undefined behavior.
                          Of course you are correct. My bad. (gads, I hate that phrase!!).
                          Thanks for the 'heads-up'.

                          <?>
                          It must have been that the char arrays were initialized to zeros by my
                          compiler.
                          <checking.... >
                          Nope!!! Just happened to be a zero at 4th position in both arrays.
                          [....even on re-compiles. ....and adding another array before those.]
                          [ I can't seem to break it! ]
                          </?>

                          Murphy's "bad luck" clause strikes again!!

                          Thanks again for the correction.
                          --
                          Bob R
                          POVrookie


                          Comment

                          • BobR

                            #14
                            Re: copy char*


                            Default User wrote in message <4s1h4hFtbh0qU1 @mid.individual .net>...
                            >BobR wrote:
                            >
                            Outputting the result was undefined behavior.
                            [ ref: the missing terminator. ]

                            HEY! How come why for you yell at me but not Jim?
                            <G>

                            --
                            Bob R
                            POVrookie


                            Comment

                            • Gary Wessle

                              #15
                              Re: copy char*

                              in order to get the clean output "abd def" I did some changes to the
                              code but now getting Seg Fault.

                              #include <string>
                              #include <iostream>
                              using namespace std;

                              class myType {
                              protected:
                              char _frst[40];
                              char _second[40];
                              char _pair[80];
                              public:
                              const char* frst() const;
                              const char* second() const;
                              myType(const char* pair);
                              };

                              myType::myType( const char* pair)
                              {
                              strncpy(_frst, pair, 3);
                              strncpy(_second , pair+4, 3);
                              }
                              const char* myType::frst()c onst {
                              char* r;
                              strncpy(r,_frst ,3);
                              return r;}
                              const char* myType::second( )const {
                              char* r;
                              strncpy(r,_seco nd,3);
                              return r;
                              }

                              int main(){
                              string s = "abc-def";
                              myType mt(s.c_str());
                              cout << mt.frst() << " " << mt.second() << endl;
                              }

                              Comment

                              Working...