Name of an array is pointer to the first element. So Why one character array cannot be assigned another array ?
#include<stdio.h>
int main()
{
char str1[]="Hello";
char str2[10];
char *s="Good Morning";
char *q;
str2=str1; /* Error */
q=s; /* Works */
return 0;
}
Arrays in expressions automatically converted to pointer pointing to the first element of the array except for operands of sizeof
operator and unary &
operator, so you cannot assign to arrays.
Adding #include <string.h>
to the head of your code and using strcpy()
is one of good ways to copy strings.
#include<stdio.h>
#include<string.h>
int main(void)
{
char str1[]="Hello";
char str2[10];
char *s="Good Morning";
char *q;
strcpy(str2, str1); /* Works */
q=s; /* Works */
return 0;
}