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. Other Discussions
  3. The Weird and The Wonderful
  4. One-Liner

One-Liner

Scheduled Pinned Locked Moved The Weird and The Wonderful
tutorial
12 Posts 9 Posters 74 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.
  • R Offline
    R Offline
    Rob Grainger
    wrote on last edited by
    #1

    A simple example of how not to copy a string... sprintf(packet.data.header.filter, "%s", "wild"); it works, I suppose. Rob Grainger

    B J 2 Replies Last reply
    0
    • R Rob Grainger

      A simple example of how not to copy a string... sprintf(packet.data.header.filter, "%s", "wild"); it works, I suppose. Rob Grainger

      B Offline
      B Offline
      Blake Miller
      wrote on last edited by
      #2

      The ones I love are these (assuming 8-bit characters) : strncpy( szDestination, szSource, sizeof(szSource) ); or strncpy( szDestination, szSource, strlen(szSource) ); or strncpy( szDestination, szSource, strlen(szDestination) ); X|

      B R 2 Replies Last reply
      0
      • B Blake Miller

        The ones I love are these (assuming 8-bit characters) : strncpy( szDestination, szSource, sizeof(szSource) ); or strncpy( szDestination, szSource, strlen(szSource) ); or strncpy( szDestination, szSource, strlen(szDestination) ); X|

        B Offline
        B Offline
        Brady Kelly
        wrote on last edited by
        #3

        For us noobs, what is wrong with the last one? At least it uses the destination length.

        D J H 3 Replies Last reply
        0
        • B Brady Kelly

          For us noobs, what is wrong with the last one? At least it uses the destination length.

          D Offline
          D Offline
          DavidNohejl
          wrote on last edited by
          #4

          I am noob too, but I dare to guess that it loses terminating zero.


          [My Blog]
          "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
          "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

          B 1 Reply Last reply
          0
          • D DavidNohejl

            I am noob too, but I dare to guess that it loses terminating zero.


            [My Blog]
            "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
            "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

            B Offline
            B Offline
            Brady Kelly
            wrote on last edited by
            #5

            Ah, yes.

            1 Reply Last reply
            0
            • B Brady Kelly

              For us noobs, what is wrong with the last one? At least it uses the destination length.

              J Offline
              J Offline
              JudyL_MD
              wrote on last edited by
              #6

              It works if the current contents of the destination are smaller than the source string. It won't copy entirely if the current destination contents are less than the source, even if the buffer is physically large enough to hold the source. By using strlen, it does not use the full available length of the destination buffer. strlen versus sizeof Judy

              B 1 Reply Last reply
              0
              • J JudyL_MD

                It works if the current contents of the destination are smaller than the source string. It won't copy entirely if the current destination contents are less than the source, even if the buffer is physically large enough to hold the source. By using strlen, it does not use the full available length of the destination buffer. strlen versus sizeof Judy

                B Offline
                B Offline
                Blake Miller
                wrote on last edited by
                #7

                Spot on :-D

                1 Reply Last reply
                0
                • B Blake Miller

                  The ones I love are these (assuming 8-bit characters) : strncpy( szDestination, szSource, sizeof(szSource) ); or strncpy( szDestination, szSource, strlen(szSource) ); or strncpy( szDestination, szSource, strlen(szDestination) ); X|

                  R Offline
                  R Offline
                  Robert Surtees
                  wrote on last edited by
                  #8

                  back in K&R C days this was always special: memcpy( dest, src );

                  1 Reply Last reply
                  0
                  • B Brady Kelly

                    For us noobs, what is wrong with the last one? At least it uses the destination length.

                    H Offline
                    H Offline
                    hatelamers
                    wrote on last edited by
                    #9

                    well, imagine that: char szDestination[] = { 0xFF, 0xFF }; strncpy(szDestination, szSource, strlen(szDestination)); what do you think would be strlen() here? -- modified at 22:38 Thursday 1st November, 2007

                    S 1 Reply Last reply
                    0
                    • H hatelamers

                      well, imagine that: char szDestination[] = { 0xFF, 0xFF }; strncpy(szDestination, szSource, strlen(szDestination)); what do you think would be strlen() here? -- modified at 22:38 Thursday 1st November, 2007

                      S Offline
                      S Offline
                      Steve Hansen
                      wrote on last edited by
                      #10

                      It's an improved random generator!

                      H 1 Reply Last reply
                      0
                      • S Steve Hansen

                        It's an improved random generator!

                        H Offline
                        H Offline
                        hatelamers
                        wrote on last edited by
                        #11

                        Steve Hansen wrote:

                        It's an improved random generator!

                        ROFL I ought to patent this! Except if you do strncpy() as given you would most likely overwrite the return address or whatever comes next in enclosing function.

                        1 Reply Last reply
                        0
                        • R Rob Grainger

                          A simple example of how not to copy a string... sprintf(packet.data.header.filter, "%s", "wild"); it works, I suppose. Rob Grainger

                          J Offline
                          J Offline
                          James R Twine
                          wrote on last edited by
                          #12

                          Seen the same dumb stuff to "clear a string":

                          sprintf( pcSomeStringBuffer, "%s", "");
                          // Or Even
                          strcpy( pcSomeStringBuffer, "" );

                          Peace!

                          -=- James
                          Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                          Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                          See DeleteFXPFiles

                          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