Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   problem on strcat (http://www.go4expert.com/forums/problem-on-strcat-t7846/)

faizanh 15Dec2007 00:18

problem on strcat
 
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

shabbir 15Dec2007 12:18

Re: problem on strcat
 
Moved to C-C++ forum.

Salem 15Dec2007 13:14

Re: problem on strcat
 
> 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.


All times are GMT +5.5. The time now is 10:41.