int main()
{
char s[]="stack";
s="overflow";
}
This is not allowed.Its gives an error.But below code works fine.
int main()
{
char s[]="stack";
strcpy(s,"overflow");
}
Why this happens?
int main()
{
char s[]="stack";
s="overflow";
}
This is not allowed.Its gives an error.But below code works fine.
int main()
{
char s[]="stack";
strcpy(s,"overflow");
}
Why this happens?
The variable s
represents a pointer to the string. More specifically, it refers to the memory address of the first letter in your string "stack"
. Due to this reason, the operation s="overflow"
makes no sense. How can you set the value of s (a pointer) to a string?
Keep in mind that C is a very low-level language, so you have to be wary of things that might seem intuitive to you not behaving the way you expect them to.