What is wrong with the following code?
char *string = "adfjk";
char *string2 = "adfjk";
strcpy(string, string2);
It produces a segmentation fault.
Printable View
What is wrong with the following code?
char *string = "adfjk";
char *string2 = "adfjk";
strcpy(string, string2);
It produces a segmentation fault.
You are copying to a string literal. Basically, the destination char array needs to be writable and have enough space to accomodate the chars to be copied.
This is the most major disadvantage of using char *string = "STRING";.
Use something more like
. . . if you must.Code:char string[128] = "string";
Thank you for your help. Things make more sense to me now, I had been struggling with some of the basics like that since I have been trying to learn C.