Where is strcpy() defined? (Can't find in string.h ect)

This is a discussion on Where is strcpy() defined? (Can't find in string.h ect) within the C++ Programming forums, part of the General Programming Boards category; I know this is perhaps a dumb question. I canít find the code for strcpy() any where, anybody got any ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Location
    Bromsgrove, UK
    Posts
    7

    Where is strcpy() defined? (Can't find in string.h ect)

    I know this is perhaps a dumb question. I canít find the code for strcpy() any where, anybody got any suggestions?

    I have looked in string.h and cstring but canít find the code. I am using
    MS Visual C++ v8 and can get the function to work but want to have a look at the code.

    Just in case you are wondering why I want to look at the code, I am working through a book on C++ and it asks you to implement your own version of strcpy() and then compare it to the standard library version.

    My version,

    Code:
    void strcopy(const char* str1, char* str2)
    {
    	while(*str2++ = *str1++);
    }
    No I didn't come up with that myself (far to clever for me), but I think I understand how it works.

    During my many searches of Google for information on this problem I have come across the fact that strcpy() is supposed to return a char*, why is this useful?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    The .h file are only header files and do not contain code for the functions, only the prototypes necessary for the compiler to do a proper job of typechecking. The actual code itself is compiled into libraries which the linker combines with the code you wrote and compiled seperately into a fully functional executable.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Apr 2006
    Location
    Bromsgrove, UK
    Posts
    7
    Thanks for the quick reply. So does that mean that Iím not going to be able to find the source for the strcpy function anywhere?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Microsoft does not use a loop like that. strcpy() is implemented in assembly code and takes advantage of the Invell chips string handling assembly functions because they are faster.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Location
    Bromsgrove, UK
    Posts
    7
    OK thanks.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by Zero_Point
    During my many searches of Google for information on this problem I have come across the fact that strcpy() is supposed to return a char*, why is this useful?
    So you could do something like this
    Code:
    strcat(strcpy(Dest, Source), AnotherString);

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    No I didn't come up with that myself (far to clever for me), but I think I understand how it works.
    There are three important points you need to know:

    1) When you march a pointer along an array, there is nothing to stop you from marching right past the end of the array and into memory that is not part of the array, and therefore that memory could contain anything. If you are lucky, marching past the end of an array and into memory that is not part of the array, will cause your program to crash. That's lucky because then you will know something is wrong with your program. If you are unlucky, your program will continue executing with some junk value.

    2) c-style strings are char arrays that end in a \0, e.g.:

    abc\0

    When you write a statement like this:

    char myStr[] = "abc";

    the compiler first takes the "string literal"(i.e. anything between double quotes) and slaps a \0 on the end of it and stores it in memory. Then it performs a strcpy() into the myStr array, which also copies the \0.

    3) A "boolean context" is some place in the code where a value must be interpreted as true or false, e.g.:

    if( ..boolean context in here...)

    or

    while( ...boolean context in here... )

    In a boolean context, the character '\0' will evaluate to false and any other character will evaluate to true. You can play around with this if statement to demonstrate that:
    Code:
    if('\0')
    {
    	cout<<"true"<<endl;
    }
    else
    {
    	cout<<"false"<<endl;
    }
    Try substituting some different characters in the if condition.

    In this code:
    Code:
    void strcopy(const char* str1, char* str2)
    {
    	while(*str2++ = *str1++);
    }
    The first thing that happens is that str1 is dereferenced to get the value it is pointing to(I'm fudging a little bit here as you will recognize if you look at an operator precedence table). Then that value is assigned to the value that str2 is pointing to, which is *str2. Then *str2 is evaluated in the boolean context. It's the same as if you did this:
    Code:
    int num;
    
    if( num = 10)
    {
         //do something
    }
    That if statement does not check if num is equal to 10. Instead, it assigns 10 to num and then evaluates num in the boolean context. In that if statement, any number that is non-zero will evaluate to true, so that if statement is true. In your while statement:
    Code:
    void strcopy(const char* str1, char* str2)
    {
    	while(*str2++ = *str1++);
    }
    if *str2 was assigned any character besides '\0', *str2 will evaluate to true. Finally, both str2 and str1 are advanced along their respective arrays. The while loop will ultimately terminate when *str1 is a '\0' character: the '\0' character will get assigned to *str2, which will be evaluated in the boolean context with the result being false. Note that by assumption you are dealing with c-strings, so str1 will end in a '\0', and you will not go marching off the end of the str1 array.
    Last edited by 7stud; 04-03-2006 at 05:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. MinGW - __cplusplus not being defined?
    By Mario F. in forum Tech Board
    Replies: 5
    Last Post: 12-04-2006, 06:17 PM
  3. Replies: 5
    Last Post: 04-16-2004, 01:29 AM
  4. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  5. Won't Return pointer, i can't find why...
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 07:50 PM

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