Hello everyone, i am trying to copy the elements of a const char array to a char array using strcpy function.When i compile my compiler does not returns me any error,but when i run it it crashes .Take a look :
Even though you are declaring c to be a char* you are assigning it to memory that is constant and can not be changed. C++ is being lenient with the type in allowing you to assign a const char* address to a non const variable.
So the compiler is not complaining but the hardware is.
Would please suggest me any exception method to prevent undesirable situations like this? Is there something that i could throw to detect the crash?? Thanks.
This is a horrible leak in C++'s type system. There may be a compiler switch to issue a warning or an error in this situation. Personally, I use std::string ;o)