What is wrong with the following code?
char *string = "adfjk";
char *string2 = "adfjk";
strcpy(string, string2);
It produces a segmentation fault.
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
This is the most major disadvantage of using char *string = "STRING";.
Use something more like
. . . if you must.Code:char string[128] = "string";
Do as I say, not as I do . . .
Experimentation is the essence of programming. Just remember to make a backup first.
"I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF
Questions posted by these guidelines are more likely to be answered.
Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
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.