Results 1 to 19 of 19

Thread: strcat, strcpy..........doesn't work

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166

    strcat, strcpy..........doesn't work

    I can't get strcat and strcpy to work.......see the code below:

    Code:
    char word[10] = "hello";
    char word2[10] = "hi again";
    
    char word3[2];
    
    strcpy(word3, word[0])
    strcat(word3, word2[0])
    
    cout << word3;
    
    should output: hh
    I've checked the tutorial, but I can't get it to work anyway.......

    any ideas?

    thanks!
    [p r a e t o r i a n]

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    that won't work, word[0] and word2[0] are char's not C strings.
    you could do
    word3[0]=word[0];
    word3[1]=word2[0];
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    Ok, thanks!

    But I wanted to use strcat and strcpy.........and not what you suggested, but thanks anyway
    [p r a e t o r i a n]

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    strcat and strcpy works only with C strings, if you want to go with C strings then you need a buffer two which you copy the substrings "h" from word and "h" from word2
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I wonder why people want to use a more complicated approach...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    jim mcnamara
    Guest
    Code:
    char word[10] = "hello";
    char word2[10] = "hi again";
    
    char word3[21];
    memset(word3,0x0,sizeof(word3));
    strcpy(word3, word);
    strcat(word3, word2);
    
    cout << word3;
    
    word3[0=word[0];
    word3[1]=word2[0];
    word3[2]=0x0;
    cout<< word3 << endl;

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Originally posted by jim mcnamara
    Code:
    word3[2]=0x0;
    Yeah, this is important!
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166

    More problemo

    problems with arrays and strcat........again!

    Code:
    char word[10] = "Pointer";
    char word2[20] = "Procrastinate";
    
    char word3[2];
    
    word3[0] = word[0];
    word3[1] = word2[0];
    word3[2] = 'r';
    
    char word4[10] = "problem";
    
    strcat(word3, word4);
    
    cout << word3;
    Should output: PPrproblem, but doesn't do that.......any ideas? and why?

    thanks again!
    [p r a e t o r i a n]

  9. #9
    jim mcnamara
    Guest
    Yes. word3 only holds 2 characters.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    so, Should I change word3[2] to word3[3] and the problems will disappear?

    I get a lot of strange characters between the two vectors when I use strcat..........???
    [p r a e t o r i a n]

  11. #11
    jim mcnamara
    Guest
    char word3[21];

  12. #12
    jim mcnamara
    Guest
    use
    memset(word3,0x0,sizeof(word3));

    C string depend on there being a null terminating character.
    In other words for strcat, strcpy and strWHATEVER() to work, there must be an ASCII 0 character at the end of the string.
    Start out by setting the whole string to ASCII 0 - what memset does above. It always works.

  13. #13
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Humm, i don't like this, if you want to stay with C strings then do so but don't mix in char's.

    Jim, i think the functions will put the terminating null themself won't they?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166

    thanks - tack

    Jim:
    Thanks, I think I get it now!

    I think I've to read up a little on c-strings, I mix up everything all the time..........

    Thanks for helping me and your patience!
    [p r a e t o r i a n]

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Basically a char array must have at least as much space as is needed for all the characters you want to fill in + 1:

    word3[0] = word[0];
    word3[1] = word2[0];
    word3[2] = 'r';
    char word4[10] = "problem";

    strcat(word3, word4);

    You need at least char word3[11] for this.

    Keda: strcat will add a '\0' at the end, but after this:
    word3[0] = word[0];
    word3[1] = word2[0];
    word3[2] = 'r';
    there is no '\0' at the end, therefore strcat does not know where to put the string.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16
    jim mcnamara
    Guest
    Kedaman -

    Some string functions don't play fair -- strncpy() for example. It does NOT put a terminating null character.

    This is why calloc() exists - it stands for character allocation - it fills allocated memory with nulls.

  17. #17
    jim mcnamara
    Guest
    (after reading CB's post)

    Plus, strcpy() is the only function smart enough to place the source string correctly into the destination string when the destination string is full of garbage. strcat(), strncat(), etc must start at a terminating null. Using unitialized strings, then some string functions on top of it-- that is a great way to trash memory.

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166

    woooorkkkkkkks!

    Thanks for your help guys! It works! it works!

    thanks for your patience
    [p r a e t o r i a n]

  19. #19
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by jim mcnamara
    Kedaman -

    Some string functions don't play fair -- strncpy() for example. It does NOT put a terminating null character.

    This is why calloc() exists - it stands for character allocation - it fills allocated memory with nulls.
    What a disappointment how about
    PHP Code:
    inline charstrncpy2(charto,const charfromint n){
        *(
    to+n+1)=0;
        return 
    strncpy(to,from,n);
    }; 
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width