-3

well i have two char array . when i try to concatenate both string using strcat function. Then my string "a" length reduced from 9 to 6. i also lost my string "a" .string b changed too.See in the output. why this is happening ???

here is what i have done

#include <bits/stdc++.h>

using namespace std;
int main() {
    char a[]="roomies!!";
    char b[]="hey kammo DJ ";
    char *c;
    c=new char[50];
    cout<<"before:-\n";
    cout<<"len of a is "<<strlen(a)<<'\n';
    cout<<"len of b is "<<strlen(b)<<'\n';
    cout<<"len of c is "<<strlen(c)<<'\n';
    cout<<"string a is = "<<a<<'\n';
    cout<<"string b is = "<<b<<'\n';
    cout<<"string c is = "<<c<<'\n';
    c=strcat(b,a);
    cout<<"\nafter:-\n";
    cout<<"len of a is "<<strlen(a)<<'\n';
    cout<<"len of b is "<<strlen(b)<<'\n';
    cout<<"len of c is "<<strlen(c)<<'\n';
    cout<<"string a is = "<<a<<'\n';
    cout<<"string b is = "<<b<<'\n';
    cout<<"string c is = "<<c<<'\n';
return 0;
}

output:-

before:-
len of a is 9
len of b is 13
len of c is 3
string a is = roomies!!
string b is = hey kammo DJ
string c is = =

after:-
len of a is 6
len of b is 22
len of c is 22
string a is = mies!!
string b is = hey kammo DJ roomies!!
string c is = hey kammo DJ roomies!!
Cœur
  • 37,241
  • 25
  • 195
  • 267
Rakesh Sharma
  • 39
  • 1
  • 7

4 Answers4

3

According to strcat spec:

"The behavior is undefined if the destination array is not large enough for the contents of both src and dest and the terminating null character."

Your destination array is "b", which is obviously not large enough to store contents of both "a" and "b", so you got an undefined behavior which resulted in modification of "a" string.

K. Kirsz
  • 1,384
  • 10
  • 11
2

strcat() appends source string to the destination string and returns the destination string.

char * strcat ( char * destination, const char * source ); 

So your statement

c = strcat(b,a) 

So array b and c will have the same values. strcat()

EDIT : "a" changed because you're overflowing "b" array. Since its c++ you can just use std::string instead of a character array.

std::string a = "hi" ; 
std::string b = "this is concat" ;
std::string c = a + b ;
Anand Srinivasan
  • 450
  • 5
  • 20
  • got that . but my orignal question is why string "a" changed. while using strcat function. – Rakesh Sharma Jun 07 '17 at 10:05
  • @RakeshSharma You didn't get it ... strcat does not return a new char array. b is changed (by appending a). – deviantfan Jun 07 '17 at 10:06
  • @deviantfan why "string "a" is changed..! – Rakesh Sharma Jun 07 '17 at 10:09
  • @RakeshSharma You know, changing your question after the answer doesn't help anyone. ... Answer: UB. Your code has it multiple times. – deviantfan Jun 07 '17 at 10:11
  • @RakeshSharma "b" changed because that is your destination array. If your question is why "a" changes, it might be because array "b" is not enough to hold the concatenated string. In that case, behavior is undefined. – Anand Srinivasan Jun 07 '17 at 10:17
1

strcat on C++ reference

Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.

You may use std::string for desired results.

Keyu Gan
  • 711
  • 5
  • 17
1

Function strcat has the signature char *strcat( char *dest, const char *src ) and appends the content of string src at the end of the string where dest points to, i.e. it alters the content of the memory to which dest points. This requires that the memory to which dest points is large enough to hold both strings src and dest.

Hence, your call strcat(b,a) actually yields undefined behaviour, as the memory block represented by b is capable of holding 14 bytes (i.e. the length of "hey kammo DJ "+ 1), but not for any additional string.

So you'd rather write something like:

strcpy (c,b);
strcat (c,a);

or:

snprintf(c, 50, "%s%s", b, a);
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58