Possible Duplicate:
What is the difference between char s[] and char *s in C?
int main()
{
char *t1 = "Hi hello";
char t2[] = " strcat";
printf("%s", strcat(t1, t2));
}
When I run this program it gives me segmentation fault
int main()
{
char t1[] = "Hi hello";
char *t2 = " strcat";
printf("%s", strcat(t1, t2));
}
Whereas when I run the next program it runs properly and shows up the concatenated string. Why is that so ?
Thanks in advance :)
SUMMARY OF THIS QUESTION [SOLVED]
This question is very much closed. Just wanted to add summary. The points which I understood are: For variables declared in this manner
char *t1 = "hi hello";
Just make sure to add type qualifier const. Since by default it is read-only memory. At any cost we cannot modify the data. For example
t1[0] = "L";
is disallowed. Variables declared in this manner are not under our control and will remain forever during the life time of program. We cannot even free that memory.
char t1[10] = "hi hello";
This way we have a better control of the memory. We are permitted to modify the array. Whenever the scope is gone, the allocated memory gets deallocated.
char t1[] = "hi hello"; char t2[8] = "hi hello";
The two declarations t1 and t2 are very much same with 8 character locations allocated sequentially.
Hope I made sense. If not please edit. :)