h! friends
i am not gettin output for below program..
it shows segmentation fault...:-O
can any one suggest me on this....

#include<stdio.h>
#include<string.h>
int main()
{
char *str1="United";
char *str2="front";
char *str3;
str3=strcat(str1,str2);
printf("\n%s",str3);
return 0;
}

thanks:)

Recommended Answers

All 2 Replies

strcat() appends one string to the end of the other string. Before that will work you have to insure the destination string is big enough to hold both strings.

str3 is not needed, so just get rid of it.

The second problem with your program is that string literals can not be changed because they normally reside in read-only memory.

To correct the able problems

include<stdio.h>
#include<string.h>
int main()
{
char str1[126] ="United";
char *str2="front";
strcat(str1,str2);
printf("\n%s",str1);
return 0;
}

To make it for generic you can read 2 strings , calculate their lenght , allocate sufficient memory using malloc to hold both of them and then call strcat.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.