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. my strcpy funcution!

my strcpy funcution!

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 5 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.
  • W Offline
    W Offline
    wbgxx
    wrote on last edited by
    #1

    char *my_strcpy(char *p1, char *p2) { if (p2 == NULL ) return p1; if (p1 == NULL) return 0; else { char *pp1 = p1; char *pp2 = p2; while (p2 != '\0') { *p1++ = *p2++; } *pp1 = '\0'; } return p1; } Is there something wrong in my function and why? Thanks in advantage

    W J L D 4 Replies Last reply
    0
    • W wbgxx

      char *my_strcpy(char *p1, char *p2) { if (p2 == NULL ) return p1; if (p1 == NULL) return 0; else { char *pp1 = p1; char *pp2 = p2; while (p2 != '\0') { *p1++ = *p2++; } *pp1 = '\0'; } return p1; } Is there something wrong in my function and why? Thanks in advantage

      W Offline
      W Offline
      wbgxx
      wrote on last edited by
      #2

      char *my_strcpy(char *p1, char *p2)
      {
      if (p2 == NULL )
      return p1;
      if (p1 == NULL)
      return 0;
      else
      {

      		char \*pp1 = p1;
      		char \*pp2 = p2;
      		while (p2 != '\\0')
      		{
      			\*p1++ = \*p2++;
      		}
      		\*pp1 = '\\0';
      	}
      	
      	return p1;
      

      }

      1 Reply Last reply
      0
      • W wbgxx

        char *my_strcpy(char *p1, char *p2) { if (p2 == NULL ) return p1; if (p1 == NULL) return 0; else { char *pp1 = p1; char *pp2 = p2; while (p2 != '\0') { *p1++ = *p2++; } *pp1 = '\0'; } return p1; } Is there something wrong in my function and why? Thanks in advantage

        J Offline
        J Offline
        Jonathan Davies
        wrote on last edited by
        #3

        wbgxx wrote:

        Is there something wrong in my function

        Have you tested it? What makes you think there's something wrong with it?

        W 1 Reply Last reply
        0
        • J Jonathan Davies

          wbgxx wrote:

          Is there something wrong in my function

          Have you tested it? What makes you think there's something wrong with it?

          W Offline
          W Offline
          wbgxx
          wrote on last edited by
          #4

          sorry,I can not test it ,can you tell me how to do it ?

          J M 2 Replies Last reply
          0
          • W wbgxx

            sorry,I can not test it ,can you tell me how to do it ?

            J Offline
            J Offline
            Jonathan Davies
            wrote on last edited by
            #5

            Pretty much as shown here http://www.space.unibe.ch/comp_doc/c_manual/C/FUNCTIONS/strcpy.html[^]

            1 Reply Last reply
            0
            • W wbgxx

              sorry,I can not test it ,can you tell me how to do it ?

              M Offline
              M Offline
              Maximilien
              wrote on last edited by
              #6

              wbgxx wrote:

              sorry,I can not test it ,can you tell me how to do it ?

              Well, how do you intend to become a developper ?? Anyway, you have a couple of choices. - simulate it on paper (run it by hand, line by line with a pencil and some paper). - run it in a debugger (Visual Studio has a good one). Max.

              Watched code never compiles.

              1 Reply Last reply
              0
              • W wbgxx

                char *my_strcpy(char *p1, char *p2) { if (p2 == NULL ) return p1; if (p1 == NULL) return 0; else { char *pp1 = p1; char *pp2 = p2; while (p2 != '\0') { *p1++ = *p2++; } *pp1 = '\0'; } return p1; } Is there something wrong in my function and why? Thanks in advantage

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Well try stepping through it with your eyes (aka reading) one line at a time. Then when you get to the line that reads

                *p1++ = *p2++;

                ask yourself what is the purpose of pointer pp1. This looks a bit like the sort of confusion that arises from not using useful variable names.

                It's time for a new signature.

                1 Reply Last reply
                0
                • W wbgxx

                  char *my_strcpy(char *p1, char *p2) { if (p2 == NULL ) return p1; if (p1 == NULL) return 0; else { char *pp1 = p1; char *pp2 = p2; while (p2 != '\0') { *p1++ = *p2++; } *pp1 = '\0'; } return p1; } Is there something wrong in my function and why? Thanks in advantage

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  The first two if() tests are not necessary. Also, pp2 is not necessary.

                  wbgxx wrote:

                  while (p2 != '\0')

                  This will not terminate like you want it to. Richard hints at the last piece of the puzzle.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "Man who follows car will be exhausted." - Confucius

                  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