0

Here is a code:

#include<stdio.h>
#include<string.h>
int main(void)
{
char *s1="hello";
char *s2="world";
char *s3;
s3=strcat(s1,s2);
printf("%s",s3);
}

What does strcat returns? Why does this code results in run time error?
And one more question : Are "hello" and "world" terminated with '\0' in case of char pointers? Please describe a bit.

4
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by Ancient Dragon
0

>What does strcat returns?
The destination source
>Why does this code results in run time error?
Because s1 is a read only string which you can not modify and that's what you're trying to do.
>Are "hello" and "world" terminated with '\0' in case of char pointers? Please describe a bit.
"hello" and "world" are literal strings, therefore it must be terminated with '\0' null terminator.

0

Note that even if s1 were not a string literal the program would not work because s1 was not allocated enough memory to contain both s1 and s2 at the same time.

char s1[80] = "Hello ";
char* s2 = "world";
strcat(s1,s2); // ok now
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.