C lang. function to copy one string to another...

Discussion in 'Software' started by Robster12, Jan 28, 2005.

  1. Robster12

    Robster12 The Horse Whisperer

    Oh no! I hope I'm not too late! I see an error! I hope this can be edited
    in time! My apologies to all who are inconvenienced by this.

    How could I have overlooked this? I did not call the function in main()!
    Mods: If you wan't to delete this post, is useless, a wasted post.
    I'll be more careful in the future!

    Robert

    am trying HARD to write a function that will copy one string into another string
    in a function. I can do it in the main(), but I want to do it in a function outside of
    main(). I have been trying now for three days and can't see why the following won't work. If this is an easy question for someone here, I would appreciate help, otherwise I am simply going to drive on, and consider that strcpy() is what is really used for this. (I just wanted to try to get it, you know).

    Here is the closest that I have got so far. This returns no error messages on compilation, its just that it seems to be not writing the new values to the second string, the one that is being written to.



    Code:
         1
         2
         3  /* got_to_work.c */
         4  /* this is a fresh start at trying to do exercise9_5 in the */
         5  /* 21 Days book. */
         6  /* Write a function that copies one array of characters */
         7  /* into another.  (Hint: Do this just as in the programs you */
         8  /* wrote on Day 8). */
         9
        10  #include<stdio.h>
        11
        12  char source[32] = "This is the source string.     ";
        13  char dest[32] =   "This is the destination string.";
        14                   /*12345678901234567890123456789012*/
        15                   /* there are 32 slots in each array */
        16  char *p_source = &source[0];
        17  char *p_dest = &dest[0];
        18
        19  void good_function(char *p_source, char *p_dest);
        20
        21  int main(void)
        22  {
        23          puts("The stings in thier origninal state: \n");
        24          puts(source);
        25          puts(dest);
        26
        27          puts("\nNow, we copy source into dest");
        28
        29          puts("here they are after that is done: \n");
        30
        31          puts(source);
        32          puts(dest);
        33
        34          return 0;
        35  }
        36
        37  void good_function(char *p_source, char *p_dest)
        38  {
        39          int count;      /* a counter for the loop */
        40          for (count = 0; count < 32; count++)
        41          {
        42                  *p_dest = *p_source;
        43                  ++p_dest;
        44                  ++p_dest;
        45          }
        46          *p_dest = '\0';
        47  }
        48
     
  2. Robster12

    Robster12 The Horse Whisperer

    No, that didn't do it. Here is the closest I've got so far, if anyone is interested:


    Code:
         1
         2
         3  /* desperate.c */
         4  /* this is a fresh start at trying to do exercise9_5 in the */
         5  /* 21 Days book. */
         6  /* Write a function that copies one array of characters */
         7  /* into another.  (Hint: Do this just as in the programs you */
         8  /* wrote on Day 8). */
         9
        10  #include<stdio.h>
        11
        12  char source[32] = "This is the source string.     ";
        13  char dest[32] =   "This is the destination string.";
        14                   /*12345678901234567890123456789012*/
        15                   /* there are 32 slots in each array */
        16  char *p_source = &source[0];
        17  char *p_dest = &dest[0];
        18
        19  void good_function(char *p_source, char *p_dest);
        20
        21  int main(void)
        22  {
        23          puts("The stings in thier origninal state: \n");
        24          puts(source);
        25          puts(dest);
        26
        27          puts("\nNow, we copy source into dest");
        28
        29          void good_function(char *p_source, char *p_dest);
        30
        31          puts("here they are after that is done: \n");
        32
        33          puts(source);
        34          puts(dest);
        35
        36          return 0;
        37  }
        38
        39  void good_function(char *p_source, char *p_dest)
        40  {
        41          while (1)
        42          {
        43                  *p_dest = *p_source;
        44
        45                  /* exit if we copied the end of the string */
        46                  if (*p_dest == '\0')
        47                          break;
        48
        49                  ++p_dest;
        50                  ++p_dest;
        51          }
        52  }
        53
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds