• Advertisement

Archived

This topic is now archived and is closed to further replies.

strcat()

This topic is 5634 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why doesn''t strcat work if the target character array is empty? See below:- char string[40],bigstring[400]; puts("Please enter your first string: "); gets(string); strcat (bigstring,string); This doesn''t work. I have included string.h in my header files. Do I have to use strcpy() for the first string and then for every string thereafter use strcat(). If so why? Why can''t I use strcat() straight off the bat?

Share this post


Link to post
Share on other sites
Advertisement
Sounds like the char array is on the stack, so filled with nonsense values to start with.
The first string for strcat must end with a null-terminator.
You can fix that by doing bigstring[0] = ''\0'' before calling strcat.

Share this post


Link to post
Share on other sites
strcat has to concatenate null terminated strings. bigstring isn''t null terminated.

char string[40],bigstring[400];

puts("Please enter your first string: ");
gets(bigstring);
puts("Please enter your second string: ");
gets(string);
strcat (bigstring,string);

printf("%s\n",bigstring);

The above will work because bigstring will contain something. Paradigm Shifter''s suggestion should also work.

Share this post


Link to post
Share on other sites

  • Advertisement