I was asked in an interview.how can we change the values of character array and a pointer to a string.
Here is my Code snippet.
#include<string.h>
int main()
{
char ch[]="abc";
char *ptr="xyz";
ptr="xyz";
//strcpy(ptr,"xyz") is giving segmentation fault
//ch="ABC"; // is throwing error: incompatible types in assignment
strcpy(ch,"ABC");
return 0;
}
Could some one please explain how ptr="xyz" is working when ch="ABC" is throwing an error and strcpy(ptr,"xyz") is giving segmentation fault and strcpy(ch,"ABC") is working fine.