Thread: String copying

  1. #1
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122

    Lightbulb String copying

    Right, this is a kinda continuation of the last post, but in a simpler method!

    Basically, i want to (in psuedo-code):

    Code:
    string1 = ".example";
    string2 = "";
    
    /* copy string1 to string2 ignoring the first char */
    string2 = "example";
    Does anybody know how i can do this???

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    In the header "string.h" exists a function called strncpy.
    Code:
    strncpy(string2, string1, strlen(string1));
    I like to play pocket pool.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Does anybody know how i can do this???
    Code:
    strcpy( string2, string1+1 );

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    first of all, the destination cannot be a string literal like you posted. Any attempt to change string literals will probably crash your program. You need to create a character array
    Code:
    char string2[10];
    char * string1 = ".example";
    strcpy(string2, string1+1);
    If you want to add more text to the end of string2 then you have to increase its size from 10 to however big you think it might need. Exact size is not important as long as the character array is at least as large as the number of characters you intend to copy there, plus 1 for the null terminator. Don't get too parinoid about wasting a few bytes of space.
    Last edited by Ancient Dragon; 11-27-2005 at 10:04 PM.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    Code:
    char string1[] = ".example";
    char string2[32];
    
    strcpy(string2, &string1[1]);
    
    printf("%s\n", string2);
    output:
    Code:
    example

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    apsync, your code is basically identical to ancient dragon's code. &string1[1] is just an ugly way of saying string1+1.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You want to see ugly? I bet I could do it with memcpy. </joke=private>


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

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by quzah
    You want to see ugly? I bet I could do it with memcpy. </joke=private>


    Quzah.
    you could also do it with a loop

  9. #9
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    Thanks people!!! ...I know that the code i posted was not valid, but i just wanted to show what i was stuck with! Now i have the answer, i can complete my sortCompare function so that it sorts the array even if elements of the array have a leading . (i.e. it ignores any leading . when it compares the array elements!)

    so the string+1 bit works perfectly!!!! thanks!!!!!

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Ancient Dragon
    you could also do it with a loop
    With memcpy in a loop...


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

  11. #11
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Yes, memcpy is best used in a loop with the third parameter set to 1, for maximum control

  12. #12
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    And just to be sure replace memcpy() with memmove() :P

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I guess I should have said (what I really meant) memset... Now that would be fun.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM