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.

Recommended Answers

All 3 Replies

>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.

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
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.