strcpy

This is a discussion on strcpy within the C++ Programming forums, part of the General Programming Boards category; Hi. Im learning c++ through bjarne stroustrup's book and in one of the exercise he offers he asks to remake ...

  1. #1
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117

    strcpy

    Hi.
    Im learning c++ through bjarne stroustrup's book and in one of the exercise he offers he asks to remake some of the functions taken from <cstring> (strlen, strcpy and strcmp)

    strlen is done.
    strcpy is almost done but I do have one question about it..

    Ive made it so far but it not the same.

    I can copy a string using this form :

    temp=copy(temp, juju);

    Of course id like to use the same form as strcpy.. :

    copy(temp,juju);

    any ideas?

    here is my code so far..

    Code:
    int main()
    {
          char* juju;
          cin >> juju; 
          cout << length(juju) << endl;     //length is the same as strlen
          char* temp = "0";
          temp=copy(temp, juju);
          cout << temp << endl;
          return 0;
    }
    
    char* copy(char* to , char* from)
    {
          to = from;
          return to;
    }
    thx
    Luigi

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    Re: strcpy

    Originally posted by Luigi

    Code:
    int main()
    {
          char* juju;      //you didn't allocate any memory for this pointer
          cin >> juju;    //crash!!!!
          cout << length(juju) << endl;    
          char* temp = "0";  //this points to a constant array containing "0"
          temp=copy(temp, juju);  
          cout << temp << endl;
          return 0;
    }
    
    char* copy(char* to , char* from)
    {
          to = from; // all this is going to do is copy the pointer.  not a string.
          return to;
    }
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    Well, first, strcpy doesn't work like you expect. You can't copy a string to a simple pointer, there has to be space for it. Second, you can't copy strings with the assignment operator. Something like this works a little better :-)
    Code:
    #include <iostream>
    
    using namespace std;
    
    char* scopy(char* to , const char* from)
    {
      char *p = to;
    
      while ((*p = *from) != '\0')
      {
        p++;
        from++;
      }
    
      return to;
    }
    
    int main()
    {
      char a[10];
      char b[] = "test";
    
      scopy(a, b);
      cout<< a <<endl;
    
      return 0;
    }
    *Cela*

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    try this:
    Code:
    #include <iostream>
    using namespace std;
    
    void copy(char *to, char *from)
       {
       char *ptr1, *ptr2;
       for(ptr1=from, ptr2 = to; *ptr1 > 0; *ptr2 = *ptr1, ptr1++, ptr2++);
       *ptr2 = 0;
       }
    
    int main(void)
       {
       char str1[256];
       char str2[256];
       cin >> str1;
       copy(str2, str1);
       cout << str2 << endl;
       return 0;
       }
    Last edited by FillYourBrain; 02-12-2003 at 07:48 PM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    I tried both code from fillyourbrain and cela.
    they both compiled well.
    but they both made the app exit befor the end with this msg error:

    ex10 has exited due to signal 10 (SIGBUS).

    it seems that all goes well until it reach cout..

    Luigi (still no clue!)

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Code:
    void copy(char *to, const char *from)
    {
        while(*to++=*from++);
    }

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    that's right, seen it done that way. duh
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Unless you have a very compelling reason not to; use std::string's

  9. #9
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Eibro
    Unless you have a very compelling reason not to; use std::string's
    Im learning c++ through bjarne stroustrup's book and in one of the exercise he offers he asks to remake some of the functions taken from <cstring> (strlen, strcpy and strcmp)

  10. #10
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    length(juju)
    Haha, you know what's funny? JuJu is slang in chinese for penis.

  11. #11
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you know, I used to be a big fan of all things stl and standard library stuff in general. but recently I've seen major speed differences in some areas. For instance in windows the ReadFile() call is unbelievably fast compared to ifstream.read(). The speed difference is incredible actually. So I've written many of my own classes to do the work of standard objects because I've done time trials on them to prove their slowness.

    Not talking about std::string necessarily but I see no reason that you shouldn't perhaps write you're own string class either.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  12. #12
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Bah, missed that... sorry.

  13. #13
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    To polymorphic :

    I dont knoow any chinese... but I do know what juju means for me..
    I took it from a game I used to play.
    In it I was a necromancer and I was controling a Zombie named Juju..
    its that simple..

    Also, I tried the code you offered and I still got a sigbus error code number 10 blah blah.. (same as previously stated..)

    Im begining to think that the problem comes from my compiler..

    (btw: project builder 2.1 for os X)

    Thx for the help.
    Luigi

  14. #14
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    It's probably from how you're using the function. You have to allocate the memory for the array before you copy!!!

  15. #15
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    Im not very familiar with new and delete yet..
    How should I use it with this code?

    like that?

    char* new juju;
    delete juju;

    ? or what ?

    Luigi

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  2. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 12:34 PM
  3. Where is strcpy() defined? (Can't find in string.h ect)
    By Zero_Point in forum C++ Programming
    Replies: 6
    Last Post: 04-03-2006, 06:14 PM
  4. Question about strcpy
    By Kevinmun in forum C Programming
    Replies: 4
    Last Post: 11-02-2005, 11:00 PM
  5. strcpy command
    By Peachy in forum C Programming
    Replies: 10
    Last Post: 09-22-2001, 10:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21