How to add a null character to the end of a string?











up vote
-1
down vote

favorite












I start of with a



memcpy(g->db_cmd,l->db.param_value.val,l->db.param_value.len);


which contains the value "function" however I want a null character to be appended like "function''" I've tried a



memcpy(&g->db_cmd[l->db.param_value.len],0,1);


This crashes the program. I've tried memset also



 memset(&g->db_cmd[l->db.param_value.len],0,1);


This doesnt work. Any idea?










share|improve this question




























    up vote
    -1
    down vote

    favorite












    I start of with a



    memcpy(g->db_cmd,l->db.param_value.val,l->db.param_value.len);


    which contains the value "function" however I want a null character to be appended like "function''" I've tried a



    memcpy(&g->db_cmd[l->db.param_value.len],0,1);


    This crashes the program. I've tried memset also



     memset(&g->db_cmd[l->db.param_value.len],0,1);


    This doesnt work. Any idea?










    share|improve this question


























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I start of with a



      memcpy(g->db_cmd,l->db.param_value.val,l->db.param_value.len);


      which contains the value "function" however I want a null character to be appended like "function''" I've tried a



      memcpy(&g->db_cmd[l->db.param_value.len],0,1);


      This crashes the program. I've tried memset also



       memset(&g->db_cmd[l->db.param_value.len],0,1);


      This doesnt work. Any idea?










      share|improve this question















      I start of with a



      memcpy(g->db_cmd,l->db.param_value.val,l->db.param_value.len);


      which contains the value "function" however I want a null character to be appended like "function''" I've tried a



      memcpy(&g->db_cmd[l->db.param_value.len],0,1);


      This crashes the program. I've tried memset also



       memset(&g->db_cmd[l->db.param_value.len],0,1);


      This doesnt work. Any idea?







      c++ c






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 16:00









      PGCodeRider

      2,0051624




      2,0051624










      asked Aug 4 '11 at 20:26









      ken

      1181410




      1181410
























          6 Answers
          6






          active

          oldest

          votes

















          up vote
          4
          down vote













          g->db_cmd[l->db.param_value.len] = 0;


          assuming you have allocated space for that.






          share|improve this answer




























            up vote
            4
            down vote













            First off, C (and C++) is not dynamic like you know it from Java, C#, PHP and others. When you are presented with a string in C, the string is pretty much static in length.



            To make the answer simpler, lets redefine your variables:





            • g->db_cmd will be called dest,


            • l->db.param_value.val will be called src, and


            • l->db.param_value.len will be called len.


            You should allocate a new string of size len plus one (for the extra null).



            Allocate a new dest:



            dest = calloc(sizeof(char), len + 1);


            calloc allocates an array of chars as long as len plus one. After calloc() has allocated the array it fills it with nulls (or ) thus you automatically will have a null appended to your deststring.



            Next, copy the src to dest with strncpy:



             strncpy(dest, src, len);


            To convert this back to your variable names:



            g->db_cmd = calloc(sizeof(char), l->db.param_value.len + 1);
            strncpy(g->db_cmd, l->db.param_value.val, l->db.param_value.len);





            share|improve this answer























            • In my opinion, memcpy() would be a more appropriate function to use than strncpy() in this example. Unless it's possible that the length of l->db.param_value.val is actually less than l->db.param_value.len. In which case, that's a confusing set of data and/or member names.
              – Michael Burr
              Aug 4 '11 at 21:39










            • Michael: you are absolutely right, given there is an Len variable. That would probably also shave of a few cycles when not checking for null in src. And Ken mentions elsewhere that he has to use memcpy(), so I should probably just change it!
              – Martin Olsen
              Aug 4 '11 at 23:00


















            up vote
            3
            down vote













            If you want string-copying semantics, why not use a string-copying function?






            share|improve this answer





















            • cant use it i need to use memcpy
              – ken
              Aug 4 '11 at 20:32






            • 4




              @ken : Why though? If this is homework, then please tag your question as such.
              – ildjarn
              Aug 4 '11 at 20:33








            • 1




              Who says the source value is NUL terminated, esp. when it's a struct with a len attribute?
              – pyroscope
              Aug 4 '11 at 20:38










            • @pyroscope : Indeed, nobody said that. Vague questions sometimes yield incorrect answers -- that's just how it goes.
              – ildjarn
              Aug 4 '11 at 20:39








            • 3




              In this case it might be reasonable to assume the source and destination are null terminated (or should be) since another question by the OP using similar code uses strlen() all over the place: stackoverflow.com/questions/6931193/concatenate-with-memcpy However, maybe that's the problem the OP was having in that question... I think the OP needs to figure out what the data they're dealing with is supposed to be.
              – Michael Burr
              Aug 4 '11 at 21:32


















            up vote
            0
            down vote













            Strings are by default null-terminated.

            If you want a to ad an extra NULL at the end you can write "String"

            or db_cmd[len]=''






            share|improve this answer

















            • 2




              Strings may be null terminated or may not be. String literals are always null terminated. </pedantic>
              – Billy ONeal
              Aug 4 '11 at 20:37






            • 1




              @Billy : I thought C++ answerers were pedantic by default? ;-]
              – ildjarn
              Aug 4 '11 at 20:38


















            up vote
            0
            down vote













            If the source you're copying from also contains a NULL terminated string use



            memcpy( g->db_cmd, l->db.param_value.val, l->db.param_value.len + 1 );


            Otherwise you'll have to add the terminator yourself.



            g->db_cmd[l->db.param_value.len] = '';


            Of course, you need to ensure that the destination has enough room for this character.






            share|improve this answer





















            • Hey, good point the value i am copying does contain it but even with the +1 it does not append ??
              – ken
              Aug 4 '11 at 21:02


















            up vote
            0
            down vote













            memcpy takes two pointers, and an integer. In the lines that you say are crashing, you pass it a pointer, and two integers. The code cannot dereference the second argument (0).

            If you really really want to use memcpy, you have to have a pointer to a zero



            char zero = 0;
            memcpy(&g->db_cmd[l->db.param_value.len], &zero , 1);


            But I would really suggest pyroscope's answer. It's faster, and clearer.






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6948107%2fhow-to-add-a-null-character-to-the-end-of-a-string%23new-answer', 'question_page');
              }
              );

              Post as a guest
































              6 Answers
              6






              active

              oldest

              votes








              6 Answers
              6






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              4
              down vote













              g->db_cmd[l->db.param_value.len] = 0;


              assuming you have allocated space for that.






              share|improve this answer

























                up vote
                4
                down vote













                g->db_cmd[l->db.param_value.len] = 0;


                assuming you have allocated space for that.






                share|improve this answer























                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  g->db_cmd[l->db.param_value.len] = 0;


                  assuming you have allocated space for that.






                  share|improve this answer












                  g->db_cmd[l->db.param_value.len] = 0;


                  assuming you have allocated space for that.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 4 '11 at 20:30









                  pyroscope

                  3,73211213




                  3,73211213
























                      up vote
                      4
                      down vote













                      First off, C (and C++) is not dynamic like you know it from Java, C#, PHP and others. When you are presented with a string in C, the string is pretty much static in length.



                      To make the answer simpler, lets redefine your variables:





                      • g->db_cmd will be called dest,


                      • l->db.param_value.val will be called src, and


                      • l->db.param_value.len will be called len.


                      You should allocate a new string of size len plus one (for the extra null).



                      Allocate a new dest:



                      dest = calloc(sizeof(char), len + 1);


                      calloc allocates an array of chars as long as len plus one. After calloc() has allocated the array it fills it with nulls (or ) thus you automatically will have a null appended to your deststring.



                      Next, copy the src to dest with strncpy:



                       strncpy(dest, src, len);


                      To convert this back to your variable names:



                      g->db_cmd = calloc(sizeof(char), l->db.param_value.len + 1);
                      strncpy(g->db_cmd, l->db.param_value.val, l->db.param_value.len);





                      share|improve this answer























                      • In my opinion, memcpy() would be a more appropriate function to use than strncpy() in this example. Unless it's possible that the length of l->db.param_value.val is actually less than l->db.param_value.len. In which case, that's a confusing set of data and/or member names.
                        – Michael Burr
                        Aug 4 '11 at 21:39










                      • Michael: you are absolutely right, given there is an Len variable. That would probably also shave of a few cycles when not checking for null in src. And Ken mentions elsewhere that he has to use memcpy(), so I should probably just change it!
                        – Martin Olsen
                        Aug 4 '11 at 23:00















                      up vote
                      4
                      down vote













                      First off, C (and C++) is not dynamic like you know it from Java, C#, PHP and others. When you are presented with a string in C, the string is pretty much static in length.



                      To make the answer simpler, lets redefine your variables:





                      • g->db_cmd will be called dest,


                      • l->db.param_value.val will be called src, and


                      • l->db.param_value.len will be called len.


                      You should allocate a new string of size len plus one (for the extra null).



                      Allocate a new dest:



                      dest = calloc(sizeof(char), len + 1);


                      calloc allocates an array of chars as long as len plus one. After calloc() has allocated the array it fills it with nulls (or ) thus you automatically will have a null appended to your deststring.



                      Next, copy the src to dest with strncpy:



                       strncpy(dest, src, len);


                      To convert this back to your variable names:



                      g->db_cmd = calloc(sizeof(char), l->db.param_value.len + 1);
                      strncpy(g->db_cmd, l->db.param_value.val, l->db.param_value.len);





                      share|improve this answer























                      • In my opinion, memcpy() would be a more appropriate function to use than strncpy() in this example. Unless it's possible that the length of l->db.param_value.val is actually less than l->db.param_value.len. In which case, that's a confusing set of data and/or member names.
                        – Michael Burr
                        Aug 4 '11 at 21:39










                      • Michael: you are absolutely right, given there is an Len variable. That would probably also shave of a few cycles when not checking for null in src. And Ken mentions elsewhere that he has to use memcpy(), so I should probably just change it!
                        – Martin Olsen
                        Aug 4 '11 at 23:00













                      up vote
                      4
                      down vote










                      up vote
                      4
                      down vote









                      First off, C (and C++) is not dynamic like you know it from Java, C#, PHP and others. When you are presented with a string in C, the string is pretty much static in length.



                      To make the answer simpler, lets redefine your variables:





                      • g->db_cmd will be called dest,


                      • l->db.param_value.val will be called src, and


                      • l->db.param_value.len will be called len.


                      You should allocate a new string of size len plus one (for the extra null).



                      Allocate a new dest:



                      dest = calloc(sizeof(char), len + 1);


                      calloc allocates an array of chars as long as len plus one. After calloc() has allocated the array it fills it with nulls (or ) thus you automatically will have a null appended to your deststring.



                      Next, copy the src to dest with strncpy:



                       strncpy(dest, src, len);


                      To convert this back to your variable names:



                      g->db_cmd = calloc(sizeof(char), l->db.param_value.len + 1);
                      strncpy(g->db_cmd, l->db.param_value.val, l->db.param_value.len);





                      share|improve this answer














                      First off, C (and C++) is not dynamic like you know it from Java, C#, PHP and others. When you are presented with a string in C, the string is pretty much static in length.



                      To make the answer simpler, lets redefine your variables:





                      • g->db_cmd will be called dest,


                      • l->db.param_value.val will be called src, and


                      • l->db.param_value.len will be called len.


                      You should allocate a new string of size len plus one (for the extra null).



                      Allocate a new dest:



                      dest = calloc(sizeof(char), len + 1);


                      calloc allocates an array of chars as long as len plus one. After calloc() has allocated the array it fills it with nulls (or ) thus you automatically will have a null appended to your deststring.



                      Next, copy the src to dest with strncpy:



                       strncpy(dest, src, len);


                      To convert this back to your variable names:



                      g->db_cmd = calloc(sizeof(char), l->db.param_value.len + 1);
                      strncpy(g->db_cmd, l->db.param_value.val, l->db.param_value.len);






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Aug 4 '11 at 21:14









                      Sean

                      2,2521726




                      2,2521726










                      answered Aug 4 '11 at 20:44









                      Martin Olsen

                      1,41611420




                      1,41611420












                      • In my opinion, memcpy() would be a more appropriate function to use than strncpy() in this example. Unless it's possible that the length of l->db.param_value.val is actually less than l->db.param_value.len. In which case, that's a confusing set of data and/or member names.
                        – Michael Burr
                        Aug 4 '11 at 21:39










                      • Michael: you are absolutely right, given there is an Len variable. That would probably also shave of a few cycles when not checking for null in src. And Ken mentions elsewhere that he has to use memcpy(), so I should probably just change it!
                        – Martin Olsen
                        Aug 4 '11 at 23:00


















                      • In my opinion, memcpy() would be a more appropriate function to use than strncpy() in this example. Unless it's possible that the length of l->db.param_value.val is actually less than l->db.param_value.len. In which case, that's a confusing set of data and/or member names.
                        – Michael Burr
                        Aug 4 '11 at 21:39










                      • Michael: you are absolutely right, given there is an Len variable. That would probably also shave of a few cycles when not checking for null in src. And Ken mentions elsewhere that he has to use memcpy(), so I should probably just change it!
                        – Martin Olsen
                        Aug 4 '11 at 23:00
















                      In my opinion, memcpy() would be a more appropriate function to use than strncpy() in this example. Unless it's possible that the length of l->db.param_value.val is actually less than l->db.param_value.len. In which case, that's a confusing set of data and/or member names.
                      – Michael Burr
                      Aug 4 '11 at 21:39




                      In my opinion, memcpy() would be a more appropriate function to use than strncpy() in this example. Unless it's possible that the length of l->db.param_value.val is actually less than l->db.param_value.len. In which case, that's a confusing set of data and/or member names.
                      – Michael Burr
                      Aug 4 '11 at 21:39












                      Michael: you are absolutely right, given there is an Len variable. That would probably also shave of a few cycles when not checking for null in src. And Ken mentions elsewhere that he has to use memcpy(), so I should probably just change it!
                      – Martin Olsen
                      Aug 4 '11 at 23:00




                      Michael: you are absolutely right, given there is an Len variable. That would probably also shave of a few cycles when not checking for null in src. And Ken mentions elsewhere that he has to use memcpy(), so I should probably just change it!
                      – Martin Olsen
                      Aug 4 '11 at 23:00










                      up vote
                      3
                      down vote













                      If you want string-copying semantics, why not use a string-copying function?






                      share|improve this answer





















                      • cant use it i need to use memcpy
                        – ken
                        Aug 4 '11 at 20:32






                      • 4




                        @ken : Why though? If this is homework, then please tag your question as such.
                        – ildjarn
                        Aug 4 '11 at 20:33








                      • 1




                        Who says the source value is NUL terminated, esp. when it's a struct with a len attribute?
                        – pyroscope
                        Aug 4 '11 at 20:38










                      • @pyroscope : Indeed, nobody said that. Vague questions sometimes yield incorrect answers -- that's just how it goes.
                        – ildjarn
                        Aug 4 '11 at 20:39








                      • 3




                        In this case it might be reasonable to assume the source and destination are null terminated (or should be) since another question by the OP using similar code uses strlen() all over the place: stackoverflow.com/questions/6931193/concatenate-with-memcpy However, maybe that's the problem the OP was having in that question... I think the OP needs to figure out what the data they're dealing with is supposed to be.
                        – Michael Burr
                        Aug 4 '11 at 21:32















                      up vote
                      3
                      down vote













                      If you want string-copying semantics, why not use a string-copying function?






                      share|improve this answer





















                      • cant use it i need to use memcpy
                        – ken
                        Aug 4 '11 at 20:32






                      • 4




                        @ken : Why though? If this is homework, then please tag your question as such.
                        – ildjarn
                        Aug 4 '11 at 20:33








                      • 1




                        Who says the source value is NUL terminated, esp. when it's a struct with a len attribute?
                        – pyroscope
                        Aug 4 '11 at 20:38










                      • @pyroscope : Indeed, nobody said that. Vague questions sometimes yield incorrect answers -- that's just how it goes.
                        – ildjarn
                        Aug 4 '11 at 20:39








                      • 3




                        In this case it might be reasonable to assume the source and destination are null terminated (or should be) since another question by the OP using similar code uses strlen() all over the place: stackoverflow.com/questions/6931193/concatenate-with-memcpy However, maybe that's the problem the OP was having in that question... I think the OP needs to figure out what the data they're dealing with is supposed to be.
                        – Michael Burr
                        Aug 4 '11 at 21:32













                      up vote
                      3
                      down vote










                      up vote
                      3
                      down vote









                      If you want string-copying semantics, why not use a string-copying function?






                      share|improve this answer












                      If you want string-copying semantics, why not use a string-copying function?







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 4 '11 at 20:31









                      ildjarn

                      55.7k7100184




                      55.7k7100184












                      • cant use it i need to use memcpy
                        – ken
                        Aug 4 '11 at 20:32






                      • 4




                        @ken : Why though? If this is homework, then please tag your question as such.
                        – ildjarn
                        Aug 4 '11 at 20:33








                      • 1




                        Who says the source value is NUL terminated, esp. when it's a struct with a len attribute?
                        – pyroscope
                        Aug 4 '11 at 20:38










                      • @pyroscope : Indeed, nobody said that. Vague questions sometimes yield incorrect answers -- that's just how it goes.
                        – ildjarn
                        Aug 4 '11 at 20:39








                      • 3




                        In this case it might be reasonable to assume the source and destination are null terminated (or should be) since another question by the OP using similar code uses strlen() all over the place: stackoverflow.com/questions/6931193/concatenate-with-memcpy However, maybe that's the problem the OP was having in that question... I think the OP needs to figure out what the data they're dealing with is supposed to be.
                        – Michael Burr
                        Aug 4 '11 at 21:32


















                      • cant use it i need to use memcpy
                        – ken
                        Aug 4 '11 at 20:32






                      • 4




                        @ken : Why though? If this is homework, then please tag your question as such.
                        – ildjarn
                        Aug 4 '11 at 20:33








                      • 1




                        Who says the source value is NUL terminated, esp. when it's a struct with a len attribute?
                        – pyroscope
                        Aug 4 '11 at 20:38










                      • @pyroscope : Indeed, nobody said that. Vague questions sometimes yield incorrect answers -- that's just how it goes.
                        – ildjarn
                        Aug 4 '11 at 20:39








                      • 3




                        In this case it might be reasonable to assume the source and destination are null terminated (or should be) since another question by the OP using similar code uses strlen() all over the place: stackoverflow.com/questions/6931193/concatenate-with-memcpy However, maybe that's the problem the OP was having in that question... I think the OP needs to figure out what the data they're dealing with is supposed to be.
                        – Michael Burr
                        Aug 4 '11 at 21:32
















                      cant use it i need to use memcpy
                      – ken
                      Aug 4 '11 at 20:32




                      cant use it i need to use memcpy
                      – ken
                      Aug 4 '11 at 20:32




                      4




                      4




                      @ken : Why though? If this is homework, then please tag your question as such.
                      – ildjarn
                      Aug 4 '11 at 20:33






                      @ken : Why though? If this is homework, then please tag your question as such.
                      – ildjarn
                      Aug 4 '11 at 20:33






                      1




                      1




                      Who says the source value is NUL terminated, esp. when it's a struct with a len attribute?
                      – pyroscope
                      Aug 4 '11 at 20:38




                      Who says the source value is NUL terminated, esp. when it's a struct with a len attribute?
                      – pyroscope
                      Aug 4 '11 at 20:38












                      @pyroscope : Indeed, nobody said that. Vague questions sometimes yield incorrect answers -- that's just how it goes.
                      – ildjarn
                      Aug 4 '11 at 20:39






                      @pyroscope : Indeed, nobody said that. Vague questions sometimes yield incorrect answers -- that's just how it goes.
                      – ildjarn
                      Aug 4 '11 at 20:39






                      3




                      3




                      In this case it might be reasonable to assume the source and destination are null terminated (or should be) since another question by the OP using similar code uses strlen() all over the place: stackoverflow.com/questions/6931193/concatenate-with-memcpy However, maybe that's the problem the OP was having in that question... I think the OP needs to figure out what the data they're dealing with is supposed to be.
                      – Michael Burr
                      Aug 4 '11 at 21:32




                      In this case it might be reasonable to assume the source and destination are null terminated (or should be) since another question by the OP using similar code uses strlen() all over the place: stackoverflow.com/questions/6931193/concatenate-with-memcpy However, maybe that's the problem the OP was having in that question... I think the OP needs to figure out what the data they're dealing with is supposed to be.
                      – Michael Burr
                      Aug 4 '11 at 21:32










                      up vote
                      0
                      down vote













                      Strings are by default null-terminated.

                      If you want a to ad an extra NULL at the end you can write "String"

                      or db_cmd[len]=''






                      share|improve this answer

















                      • 2




                        Strings may be null terminated or may not be. String literals are always null terminated. </pedantic>
                        – Billy ONeal
                        Aug 4 '11 at 20:37






                      • 1




                        @Billy : I thought C++ answerers were pedantic by default? ;-]
                        – ildjarn
                        Aug 4 '11 at 20:38















                      up vote
                      0
                      down vote













                      Strings are by default null-terminated.

                      If you want a to ad an extra NULL at the end you can write "String"

                      or db_cmd[len]=''






                      share|improve this answer

















                      • 2




                        Strings may be null terminated or may not be. String literals are always null terminated. </pedantic>
                        – Billy ONeal
                        Aug 4 '11 at 20:37






                      • 1




                        @Billy : I thought C++ answerers were pedantic by default? ;-]
                        – ildjarn
                        Aug 4 '11 at 20:38













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      Strings are by default null-terminated.

                      If you want a to ad an extra NULL at the end you can write "String"

                      or db_cmd[len]=''






                      share|improve this answer












                      Strings are by default null-terminated.

                      If you want a to ad an extra NULL at the end you can write "String"

                      or db_cmd[len]=''







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 4 '11 at 20:34









                      cprogrammer

                      3,22432345




                      3,22432345








                      • 2




                        Strings may be null terminated or may not be. String literals are always null terminated. </pedantic>
                        – Billy ONeal
                        Aug 4 '11 at 20:37






                      • 1




                        @Billy : I thought C++ answerers were pedantic by default? ;-]
                        – ildjarn
                        Aug 4 '11 at 20:38














                      • 2




                        Strings may be null terminated or may not be. String literals are always null terminated. </pedantic>
                        – Billy ONeal
                        Aug 4 '11 at 20:37






                      • 1




                        @Billy : I thought C++ answerers were pedantic by default? ;-]
                        – ildjarn
                        Aug 4 '11 at 20:38








                      2




                      2




                      Strings may be null terminated or may not be. String literals are always null terminated. </pedantic>
                      – Billy ONeal
                      Aug 4 '11 at 20:37




                      Strings may be null terminated or may not be. String literals are always null terminated. </pedantic>
                      – Billy ONeal
                      Aug 4 '11 at 20:37




                      1




                      1




                      @Billy : I thought C++ answerers were pedantic by default? ;-]
                      – ildjarn
                      Aug 4 '11 at 20:38




                      @Billy : I thought C++ answerers were pedantic by default? ;-]
                      – ildjarn
                      Aug 4 '11 at 20:38










                      up vote
                      0
                      down vote













                      If the source you're copying from also contains a NULL terminated string use



                      memcpy( g->db_cmd, l->db.param_value.val, l->db.param_value.len + 1 );


                      Otherwise you'll have to add the terminator yourself.



                      g->db_cmd[l->db.param_value.len] = '';


                      Of course, you need to ensure that the destination has enough room for this character.






                      share|improve this answer





















                      • Hey, good point the value i am copying does contain it but even with the +1 it does not append ??
                        – ken
                        Aug 4 '11 at 21:02















                      up vote
                      0
                      down vote













                      If the source you're copying from also contains a NULL terminated string use



                      memcpy( g->db_cmd, l->db.param_value.val, l->db.param_value.len + 1 );


                      Otherwise you'll have to add the terminator yourself.



                      g->db_cmd[l->db.param_value.len] = '';


                      Of course, you need to ensure that the destination has enough room for this character.






                      share|improve this answer





















                      • Hey, good point the value i am copying does contain it but even with the +1 it does not append ??
                        – ken
                        Aug 4 '11 at 21:02













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      If the source you're copying from also contains a NULL terminated string use



                      memcpy( g->db_cmd, l->db.param_value.val, l->db.param_value.len + 1 );


                      Otherwise you'll have to add the terminator yourself.



                      g->db_cmd[l->db.param_value.len] = '';


                      Of course, you need to ensure that the destination has enough room for this character.






                      share|improve this answer












                      If the source you're copying from also contains a NULL terminated string use



                      memcpy( g->db_cmd, l->db.param_value.val, l->db.param_value.len + 1 );


                      Otherwise you'll have to add the terminator yourself.



                      g->db_cmd[l->db.param_value.len] = '';


                      Of course, you need to ensure that the destination has enough room for this character.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 4 '11 at 20:40









                      Praetorian

                      86.5k10181266




                      86.5k10181266












                      • Hey, good point the value i am copying does contain it but even with the +1 it does not append ??
                        – ken
                        Aug 4 '11 at 21:02


















                      • Hey, good point the value i am copying does contain it but even with the +1 it does not append ??
                        – ken
                        Aug 4 '11 at 21:02
















                      Hey, good point the value i am copying does contain it but even with the +1 it does not append ??
                      – ken
                      Aug 4 '11 at 21:02




                      Hey, good point the value i am copying does contain it but even with the +1 it does not append ??
                      – ken
                      Aug 4 '11 at 21:02










                      up vote
                      0
                      down vote













                      memcpy takes two pointers, and an integer. In the lines that you say are crashing, you pass it a pointer, and two integers. The code cannot dereference the second argument (0).

                      If you really really want to use memcpy, you have to have a pointer to a zero



                      char zero = 0;
                      memcpy(&g->db_cmd[l->db.param_value.len], &zero , 1);


                      But I would really suggest pyroscope's answer. It's faster, and clearer.






                      share|improve this answer

























                        up vote
                        0
                        down vote













                        memcpy takes two pointers, and an integer. In the lines that you say are crashing, you pass it a pointer, and two integers. The code cannot dereference the second argument (0).

                        If you really really want to use memcpy, you have to have a pointer to a zero



                        char zero = 0;
                        memcpy(&g->db_cmd[l->db.param_value.len], &zero , 1);


                        But I would really suggest pyroscope's answer. It's faster, and clearer.






                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          memcpy takes two pointers, and an integer. In the lines that you say are crashing, you pass it a pointer, and two integers. The code cannot dereference the second argument (0).

                          If you really really want to use memcpy, you have to have a pointer to a zero



                          char zero = 0;
                          memcpy(&g->db_cmd[l->db.param_value.len], &zero , 1);


                          But I would really suggest pyroscope's answer. It's faster, and clearer.






                          share|improve this answer












                          memcpy takes two pointers, and an integer. In the lines that you say are crashing, you pass it a pointer, and two integers. The code cannot dereference the second argument (0).

                          If you really really want to use memcpy, you have to have a pointer to a zero



                          char zero = 0;
                          memcpy(&g->db_cmd[l->db.param_value.len], &zero , 1);


                          But I would really suggest pyroscope's answer. It's faster, and clearer.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Aug 4 '11 at 23:21









                          Mooing Duck

                          46.1k1072126




                          46.1k1072126






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6948107%2fhow-to-add-a-null-character-to-the-end-of-a-string%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest




















































































                              Popular posts from this blog

                              Bressuire

                              Vorschmack

                              Quarantine