Thread: Help trying to implement strcpy

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    Help trying to implement strcpy

    Code:
    void StrCpy(char * Dest , char* Src )
    {
     
      while(*Src){
        *Dest = *Src++;
        cout << *Dest ++;    
        
      }
    }
    
    void main()
    {  
    
    char* string1 = "\0";
    char * string2 = "EL";
    
    StrCpy(string1, string2);
    //cout << string1; //must output EL
    }
    Im trying to output EL after its copied to string1 but nothing its printed

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ummmmmmm you cannot just do this with string literals my friend. Elysia will **** a brick when she catches wind of this code.

    Code:
    char string1[3];

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    it worked still ...

    it works!! .. and I heard of that before but got confused cuz the thing I did was to Initialize char * string1 = "longer than string2" and evrything else the same and it didnt work either so I thought that wasnt the problem as I thought there was a solution without need to "mix" char[] and char *
    Could you tell me why using a longer string than string2 to initialize char* pointer wouldnt work in my method
    I thought I would get something like this:

    char* string 1 = "longer than string2"
    char* string 2 = "NONSENSE"

    func(string1 string2)//same as before
    cout << string1 ; //I was expecting it to print changed string1 as
    "NONSENSEhan string2"
    in other words overwritting

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you do char *string1 = "it doesn't matter what this is", you CANNOT write to the characters pointed to by string1. It will always and forever be "it doesn't matter what this is", provided an attempt to change these characters does not shut down your program. A pointer to a string literal is just that -- a pointer to a string literal; it is not a pointer to memory that you own.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Ironically, you have to use strcpy to initialize a string. So you would need this:
    Code:
    char string1[some_size], string2[some_size];
    strcpy(strin1, "\0");
    strcpy(string2, "EL");
    that is the correct way to do what you thought your code does. The reason is what tabstop said. You use a char to a string literal (what you have) when you want to use that string literal and don't want to change it.
    Like:
    Code:
    char* phrase = "Hey, dude!";
    printf("&#37;s", phrase);
    strcpy(string1, phrase);
    strcpy(string2, phrase);
    strcpy(phrase, other_phrase); //this is wrong!

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by namehere05 View Post
    it works!! .. and I heard of that before but got confused cuz the thing I did was to Initialize char * string1 = "longer than string2" and evrything else the same and it didnt work either so I thought that wasnt the problem as I thought there was a solution without need to "mix" char[] and char *
    Could you tell me why using a longer string than string2 to initialize char* pointer wouldnt work in my method
    I thought I would get something like this:

    char* string 1 = "longer than string2"
    char* string 2 = "NONSENSE"

    func(string1 string2)//same as before
    cout << string1 ; //I was expecting it to print changed string1 as
    "NONSENSEhan string2"
    in other words overwritting
    Yes, in some compilers it will allow you to do that - it's pure coincidence and bad. You SHOULD NEVER DO THAT - just because crossing a busy road whilst blindfolded may work sometimes if you are lucky, doesn't make it a good idea.

    Most compilers will put strings like that in memory that is set to read-only, which means that the OS will block those writes, and your code won't work.

    My guess is that you are dealing with a compiler that is almost old enough to pass a car driving test - Turbo C or some such. In which case it's got no memory protection at all - you can write anything to anywhere.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your function doesn't copy the null-terminator.

    And the important thing:
    Code:
    char writable[100] = "You can change this";
    char* nonwritable = "You can not change this";
    
    //for which reason the latter should always be declared as, even though the compiler doesn't require this
    const char* nonwritable = "You cannot change this";
    Also the signature of your function should show that the target will be modified but the source won't:
    Code:
    void StrCpy(char* target, const char* source);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    http://cpwiki.sf.net/void_main
    Also, have you heard of "using namespace std;"?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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. Implement "Whats This" using Win32
    By hemanth.balaji in forum Windows Programming
    Replies: 1
    Last Post: 05-29-2005, 06:03 AM