> why my compiler dont know the strcpy.
> always makes me trouble when iam using this method of strcpy
If the 'trouble' is that it generates a warning when we use
std::strcpy(),
this particular trouble may be blithely ignored.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <cstring>
#include <iostream>
int main()
{
char cstr[128] ;
// the microsoft compiler, by default, would give you a warning for this standard function.
// this warning may safely be completely ignored
//
std::strcpy( cstr, "hello world!\n" ) ;
// microsoft nonsense:
// *** warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.
// To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
// note: see declaration of 'strcpy'
std::cout << cstr ;
}
|
http://rextester.com/HCDQH47050
std::strcpy() is the standard function; it is not deprecated.
::strcpy_s() is a non-standard, non-portable extension that some library implementations provide. Any function, including the
::strcpy_s() that Microsoft trumpets, is unsafe when used in an unsafe manner.
If this warning distresses you, it can be suppressed by defining the preprocessor symbol
_CRT_SECURE_NO_WARNINGS (before any header is parsed).
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#define _CRT_SECURE_NO_WARNINGS // ask microsoft to stop complaining about
// standard functions that are not deprecated
#include <cstring>
#include <iostream>
int main()
{
char cstr[128] ;
std::strcpy( cstr, "hello world!\n" ) ;
std::cout << cstr ;
}
|
http://rextester.com/ZLF30829
As a general rule, use standard functions whenever they are available.
It is ok to write your own version of
strcpy as a purely academic exercise; once you have learned something from it, throw the home-grown version away; use
std::strcpy()