problem on strcat

Discussion in 'C++' started by faizanh, Dec 14, 2007.

  1. faizanh

    faizanh New Member

    Joined:
    Dec 14, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    hello freinds,

    i have small program as follows

    char arr[30] = "someString";
    strcat(arr,arr);
    cout<<arr<<endl;

    when i try to print arr nothing gets printed.
    Strcat fn should append the string nothing gets appended .

    please correct if iam wrong some where
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Moved to C-C++ forum.
     
  3. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    > strcat(arr,arr);
    There are very few standard C functions (memmove being a notable exception) which work reliably when the input and output buffers overlap. In essence, if you change the thing you're reading from, then expect surprises (sometimes the surprise comes years later).

    This for example is likely to just keep going until it runs off the end of memory (or trashes something) and then it crashes.

    Since you used cout, why not use std::string for all your strings. It's much safer.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice