String concatenation using pointers

I know that this is a very common problem but I am not able to print the output..
Here is my program :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std ;

void concat(char* str1 , char* str2){
  while(*str1 != '\0'){
    *str1++ ;
  }

  while(*str2 != '\0'){
    *str1++ = *str2++ ;
  }
 *str1 = '\0';
}

int main () {
 char *fn = "Raman" ;
 char *ln = "Ghai";
 char *fullname = fn ;
 concat(fn,ln);
 cout <<"\nAfter concat:\n"<<"Str1: "<<fullname;
 return 0 ;
}


Nothing prints on output .
This is incorrect. You are not allocating memory for the new string. You are trying to write on memory beyond the first constant string that you did not allocate. Chances are your program will blow out due to access violation.

Study about arrays and memory allocation before attempting string concatenation.
i think problem is that when you initializes the fn pointer its size get fixed. and when u when increment it beyond its length i.e.in the second while loop its give an invalid address access error which is an run time error that's why ur program get compiled but don't give any output. i hereby a correct code for u.i think it solves ur problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std ;
void concat(char* str1 , char* str2)
{
  while(*str1 != '\0'){
    *str1++ ;
  }
  *str1++=' ';
  while(*str2 != '\0'){
    *str1++ = *str2++ ;
  }
 *str1 = '\0';
}

int main () {
 char *fn = "Raman" ;
 char *ln = "Ghai";
 char *fullname;
 fullname=(char *)malloc(30*sizeof(char));
 strcpy(fullname,fn);
 concat(fullname,ln);
 cout <<"\nAfter concat:\n"<<fullname;
 getch();
 return 0 ;
}
Last edited on
no, the problem is that you increment the value of the first charactewr in the first string untill it rieaches \0, then you set that fisrt value to ' ', and you make the current value of the first character int the first string the first character in the second string and increment them both, untill they reach \0. So int the end, you end up with a string whose first character is \0, so nothing is printed. You need to use std::strings
@vilimil, I thought of that at first, but as it turns out, postfix operator++ has precedence over operator* (dereference). The first while is OK, although the dereferencing is a waste. The first while correctly moves to the end of the string.

The second while is also OK. The original code presented by the OP should concatenate the string just fine, provided there's allocated memory in the first string, which is not. Most likely nothing is printed on screen because the call to cout's operator<< invalidates the stream.
If nothing else, simply use strcat().
strcat() also needs memory allocated before the function call. There is just no way around the OP's problem short of the std::string class. The OP needs to learn about memory allocation and related topics.
In the end , I used std::string and used + operator for concatenation . Gotta practice more with pointers . thanks for replying guys.
Topic archived. No new replies allowed.