Thread: strcpy...i dont get it

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    1

    strcpy...i dont get it

    Im still pretty new to c++ and most of the things ive read so far, i get. But I dont get how to use strcpy and strcmp.
    Like, I dont get what it means when it says

    Code:
    char *strcpy ( char *dest, const char *src );
    
    // or
    
    int strcmp ( const char *s1, const char *s2 );
    Could someone give me a different example of this? please

    EDIT: Basically what confuses me is the *dest and *src. And whats the difference in a char and a const char?

    Im trying to figure out how to use the correctly because I want to make a game eventually(text to start with) and somewhere along the line I will likely use them. But I dont get how I would use them.
    Last edited by whoME; 08-01-2006 at 06:07 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you are using C++, you don't really need those functions. In C++ you would use the string class, which has copying and comparison like built-in types.

    For your question, const means that the data will not change. So, for strcmp, which compares two C-style strings, neither string needs to change to do the comparison. For strcpy, you are copying the string in src into the location pointed to by dest. That means that dest will change, and so dest is not const. They are all a form of char* because that is how you represent a string in C.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Since you can't do C string copying or comparisons with operators alone, we created functions to do that for us because it's easier that way.

    If you have two strings, strcpy will take the array pointed to by src and store it in dest.
    Code:
    char part[] = " world"
    char whole[13] = "hello";
    
    strcpy( whole, part );
    
    cout << whole << "\n"; // output: "hello world"
    strcmp compares two strings, and returns 0 if they are the same, >0 if str1 is greater, or <0 if str1 is less than str2.
    Code:
    char str1[] = "sage", str2[] = "set a good example";
    if ( strcmp(str1, str2) < 0 )
    {
        cout << str1 << " is less than " << str2 << "\n"
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by citizen
    Since you can't do C string copying or comparisons with operators alone, we created functions to do that for us because it's easier that way.

    If you have two strings, strcpy will take the array pointed to by src and store it in dest.
    Code:
    char part[] = " world"
    char whole[13] = "hello";
    
    strcpy( whole, part );
    
    cout << whole << "\n"; // output: "hello world"
    You're thinking of strcat.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    This is one of those situations where you'd expect to hear the wrong music from the Price Is Right.* Embarrassing.

    Code:
    char in_here[BUFSIZ];
    strcpy(in_here, "store this in here \n");
    cout << in_here;
    *Bob Barker died a long time ago, yet somehow the show hasn't changed since the seventies. So the must've stuffed his body and now he's a puppet. They drag him around on strings for every show.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    142

    Unhappy

    Quote Originally Posted by whoME
    Im still pretty new to c++ and most of the things ive read so far, i get. But I dont get how to use strcpy and strcmp.
    Like, I dont get what it means when it says

    Code:
    char *strcpy ( char *dest, const char *src );
    
    // or
    
    int strcmp ( const char *s1, const char *s2 );
    Could someone give me a different example of this? please

    EDIT: Basically what confuses me is the *dest and *src. And whats the difference in a char and a const char?

    Im trying to figure out how to use the correctly because I want to make a game eventually(text to start with) and somewhere along the line I will likely use them. But I dont get how I would use them.
    i think everyone confused him
    WhAtHA hell Is GoInG ON

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, 05:14 PM
  4. Question about strcpy
    By Kevinmun in forum C Programming
    Replies: 4
    Last Post: 11-02-2005, 11:00 PM
  5. strcpy
    By Luigi in forum C++ Programming
    Replies: 17
    Last Post: 02-16-2003, 04:11 PM