I'm studying C now.
I have tried to learn strcpy function in "string.h". But, I got a problem from that function.
Before writing a problem, I already studied that the difference between char * const
and char const *
and const char * const
.
The problem is I don't know the difference between char * const _
and char _ []
. (_
is variable name)
I wrote a code but "example 1" is correct and "example 2" isn't correct to build and execute.
[example 1]
#include <stdio.h>
#include <string.h>
int main()
{
char s1[] = "Hello";
char s2[] = "";
strcpy(s2, s1);
printf("%s %s\n", s1, s2);
return 0;
}
*** OUTPUT: Hello Hello
[example 2]
#include <stdio.h>
#include <string.h>
int main()
{
char *const s1 = "Hello";
char *const s2 = "";
strcpy(s2, s1);
printf("%s %s\n", s1, s2);
return 0;
}
*** OUTPUT: segmentation fault (core dumped)
I knew char * const is equal to array. Isn't it true? And I want to know the problem in this code.
I hope your help. Thank you.