Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Copy the string from const char* To char[500];

Copy the string from const char* To char[500];

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
15 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K ksaw123

    Hi all can you help? part of my code i need to do this

    const char * p= "ddddd";
    char q[500];

    I want to copy the sting in p to q[]?

    C Offline
    C Offline
    CPallini
    wrote on last edited by
    #4

    const char * p= "ddddd";
    char q[500];
    int i;
    i=0;
    while (p[i] && i < sizeof(q) - 1 )
    q[i++] = p[i];
    q[i]='\0';

    :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
    [My articles]

    K C 2 Replies Last reply
    0
    • C CPallini

      const char * p= "ddddd";
      char q[500];
      int i;
      i=0;
      while (p[i] && i < sizeof(q) - 1 )
      q[i++] = p[i];
      q[i]='\0';

      :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      K Offline
      K Offline
      ksaw123
      wrote on last edited by
      #5

      Thanks CPallini

      1 Reply Last reply
      0
      • C CPallini

        const char * p= "ddddd";
        char q[500];
        int i;
        i=0;
        while (p[i] && i < sizeof(q) - 1 )
        q[i++] = p[i];
        q[i]='\0';

        :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #6

        Why do you complicate your life that way :doh: Are you borde today :-D ?

        Cédric Moonen Software developer
        Charting control [v2.0] OpenGL game tutorial in C++

        C K 2 Replies Last reply
        0
        • C Cedric Moonen

          Why do you complicate your life that way :doh: Are you borde today :-D ?

          Cédric Moonen Software developer
          Charting control [v2.0] OpenGL game tutorial in C++

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #7

          Cedric Moonen wrote:

          Why do you complicate your life that way

          Actually it was pathetically simple. I'm always 'borde'! :-D

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          C 1 Reply Last reply
          0
          • C Cedric Moonen

            Why do you complicate your life that way :doh: Are you borde today :-D ?

            Cédric Moonen Software developer
            Charting control [v2.0] OpenGL game tutorial in C++

            K Offline
            K Offline
            killabyte
            wrote on last edited by
            #8

            Some would reach for the off the self "pirelli" wheel, others fashoin themselves the "pallini" wheel... both are high quality :-\

            C 1 Reply Last reply
            0
            • C CPallini

              Cedric Moonen wrote:

              Why do you complicate your life that way

              Actually it was pathetically simple. I'm always 'borde'! :-D

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #9

              CPallini wrote:

              Actually it was pathetically simple.

              Yes I know that it was simple but you are suggesting to the OP that each time he has to copy a string, he should use this code. Honnestly, I really don't think it is a good idea because it makes the code much more difficult to understand (suppose that you repeat that code in a lot of places in your code X| ). And apparently, that's what the OP is going to do instead of using one of the "built in" mechanism.

              CPallini wrote:

              I'm always 'borde'

              Mh, yeah, I meant bored of course ;P

              Cédric Moonen Software developer
              Charting control [v2.0] OpenGL game tutorial in C++

              C 1 Reply Last reply
              0
              • C Cedric Moonen

                CPallini wrote:

                Actually it was pathetically simple.

                Yes I know that it was simple but you are suggesting to the OP that each time he has to copy a string, he should use this code. Honnestly, I really don't think it is a good idea because it makes the code much more difficult to understand (suppose that you repeat that code in a lot of places in your code X| ). And apparently, that's what the OP is going to do instead of using one of the "built in" mechanism.

                CPallini wrote:

                I'm always 'borde'

                Mh, yeah, I meant bored of course ;P

                Cédric Moonen Software developer
                Charting control [v2.0] OpenGL game tutorial in C++

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #10

                Cedric Moonen wrote:

                Yes I know that it was simple but you are suggesting to the OP that each time he has to copy a string, he should use this code.

                Nope, I would suggest him to use the library functions actually. I posted the code just to show how the task could be accomplished without using such functions (as a side note to other answers).

                Cedric Moonen wrote:

                Honnestly, I really don't think it is a good idea

                I agree, because library functions are more general.

                Cedric Moonen wrote:

                because it makes the code much more difficult to understand (suppose that you repeat that code in a lot of places in your code Dead ).

                I disagree. OP should have at least a rough idea on what happens behind the curtains of the library functions. Repeating the code is a very bad practice that I would never suggest. I suppose that showing a piece of code is not a boost to 'inline' code instead of properly 'wrap the code with a function and then call it everywhere it is needed'.

                Cedric Moonen wrote:

                And apparently, that's what the OP is going to do instead of using one of the "built in" mechanism

                I'm really not so clever to guess OP intentions. Anyway I would suggest him to use library functions (that anyway are not, in my opinion, a built-in mechanism...). :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                1 Reply Last reply
                0
                • K killabyte

                  Some would reach for the off the self "pirelli" wheel, others fashoin themselves the "pallini" wheel... both are high quality :-\

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #11

                  killabyte wrote:

                  Some would reach for the off the self "pirelli" wheel, others fashoin themselves the "pallini" wheel... both are high quality

                  :laugh: Well, personally, I would never use hand-crafted tyres on my GSR. :rolleyes:

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  1 Reply Last reply
                  0
                  • K ksaw123

                    Hi all can you help? part of my code i need to do this

                    const char * p= "ddddd";
                    char q[500];

                    I want to copy the sting in p to q[]?

                    S Offline
                    S Offline
                    Stuart Dootson
                    wrote on last edited by
                    #12

                    Well, you've not had a C++ answer using STL yet...so, just because:

                    // needs #include
                    const char * p= "ddddd";
                    char q[500];

                    std::copy(p, p+strlen(p), q);

                    Of course, it is less efficient than the pure C answers, as it iterates through p twice.

                    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                    C 1 Reply Last reply
                    0
                    • S Stuart Dootson

                      Well, you've not had a C++ answer using STL yet...so, just because:

                      // needs #include
                      const char * p= "ddddd";
                      char q[500];

                      std::copy(p, p+strlen(p), q);

                      Of course, it is less efficient than the pure C answers, as it iterates through p twice.

                      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                      C Offline
                      C Offline
                      CPallini
                      wrote on last edited by
                      #13

                      Stuart Dootson wrote:

                      // needs #include const char * p= "ddddd"; char q[500]; std::copy(p, p+strlen(p), q);

                      That's hybrid!

                      // needs a different #include!
                      const char * p= "ddddd";
                      char q[500];

                      std::string s(p);
                      s += '\0';
                      s.copy(q, s.length());

                      (I know, that's, well..., ugly! :rolleyes: ) :-D

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      S 1 Reply Last reply
                      0
                      • C CPallini

                        Stuart Dootson wrote:

                        // needs #include const char * p= "ddddd"; char q[500]; std::copy(p, p+strlen(p), q);

                        That's hybrid!

                        // needs a different #include!
                        const char * p= "ddddd";
                        char q[500];

                        std::string s(p);
                        s += '\0';
                        s.copy(q, s.length());

                        (I know, that's, well..., ugly! :rolleyes: ) :-D

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        S Offline
                        S Offline
                        Stuart Dootson
                        wrote on last edited by
                        #14

                        CPallini wrote:

                        That's hybrid

                        Hybrid's[^] good these days, maaaaan (allegedly).

                        CPallini wrote:

                        // needs a different #include! const char * p= "ddddd"; char q[500]; std::string s(p); s += '\0'; s.copy(q, s.length()); (I know, that's, well..., ugly! )

                        Well, if I'd known you were bringing strings, I'd've used this:

                        const std::string p = "ddddd";
                        std::string q;
                        q=p;

                        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                        C 1 Reply Last reply
                        0
                        • S Stuart Dootson

                          CPallini wrote:

                          That's hybrid

                          Hybrid's[^] good these days, maaaaan (allegedly).

                          CPallini wrote:

                          // needs a different #include! const char * p= "ddddd"; char q[500]; std::string s(p); s += '\0'; s.copy(q, s.length()); (I know, that's, well..., ugly! )

                          Well, if I'd known you were bringing strings, I'd've used this:

                          const std::string p = "ddddd";
                          std::string q;
                          q=p;

                          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                          C Offline
                          C Offline
                          CPallini
                          wrote on last edited by
                          #15

                          Stuart Dootson wrote:

                          const std::string p = "ddddd"; std::string q; q=p;

                          std::string q = "ddddd";

                          FFY :laugh:

                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                          [My articles]

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups