首先说一下源码到底在哪里找。

我们在文件中包含<cstring>时,如果点击右键打开文档,

会打开cstring,我们会发现路径为:

D:\Program Files\visual studio\VC\include\cstring

这个文件内容如下:

  1. // cstring standard header
  2. #pragma once
  3. #ifndef _CSTRING_
  4. #define _CSTRING_
  5. #include <yvals.h>
  6.  
  7. #ifdef _STD_USING
  8. #undef _STD_USING
  9. #include <string.h>
  10. #define _STD_USING
  11.  
  12. #else /* _STD_USING */
  13. #include <string.h>
  14. #endif /* _STD_USING */
  15.  
  16. #if _GLOBAL_USING && !defined(RC_INVOKED)
  17. _STD_BEGIN
  18. using _CSTD size_t; using _CSTD memchr; using _CSTD memcmp;
  19.  
  20. using _CSTD memcpy; using _CSTD memmove; using _CSTD memset;
  21. using _CSTD strcat; using _CSTD strchr; using _CSTD strcmp;
  22. using _CSTD strcoll; using _CSTD strcpy; using _CSTD strcspn;
  23. using _CSTD strerror; using _CSTD strlen; using _CSTD strncat;
  24. using _CSTD strncmp; using _CSTD strncpy; using _CSTD strpbrk;
  25. using _CSTD strrchr; using _CSTD strspn; using _CSTD strstr;
  26. using _CSTD strtok; using _CSTD strxfrm;
  27. _STD_END
  28. #endif /* _GLOBAL_USING */
  29.  
  30. #endif /* _CSTRING_ */
  31.  
  32. /*
  33. * Copyright (c) 1992-2009 by P.J. Plauger. ALL RIGHTS RESERVED.
  34. * Consult your license regarding permissions and restrictions.
  35. V5.20:0009 */

它没有cpp文件。包含了<string.h>头文件,也在这个目录下,内容如下;

  1. /***
  2. *string.h - declarations for string manipulation functions
  3. *
  4. * Copyright (c) Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * This file contains the function declarations for the string
  8. * manipulation functions.
  9. * [ANSI/System V]
  10. *
  11. * [Public]
  12. *
  13. ****/
  14.  
  15. #pragma once
  16.  
  17. #ifndef _INC_STRING
  18. #define _INC_STRING
  19.  
  20. #include <crtdefs.h>
  21.  
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25.  
  26. #ifndef _NLSCMP_DEFINED
  27. #define _NLSCMPERROR 2147483647 /* currently == INT_MAX */
  28. #define _NLSCMP_DEFINED
  29. #endif
  30.  
  31. /* Define NULL pointer value */
  32. #ifndef NULL
  33. #ifdef __cplusplus
  34. #define NULL 0
  35. #else
  36. #define NULL ((void *)0)
  37. #endif
  38. #endif
  39.  
  40. /* For backwards compatibility */
  41. #define _WConst_return _CONST_RETURN
  42.  
  43. /* Function prototypes */
  44. #ifndef _CRT_MEMORY_DEFINED
  45. #define _CRT_MEMORY_DEFINED
  46. _CRTIMP void * __cdecl _memccpy( _Out_opt_bytecap_(_MaxCount) void * _Dst, _In_ const void * _Src, _In_ int _Val, _In_ size_t _MaxCount);
  47. _Check_return_ _CRTIMP _CONST_RETURN void * __cdecl memchr( _In_opt_bytecount_(_MaxCount) const void * _Buf , _In_ int _Val, _In_ size_t _MaxCount);
  48. _Check_return_ _CRTIMP int __cdecl _memicmp(_In_opt_bytecount_(_Size) const void * _Buf1, _In_opt_bytecount_(_Size) const void * _Buf2, _In_ size_t _Size);
  49. _Check_return_ _CRTIMP int __cdecl _memicmp_l(_In_opt_bytecount_(_Size) const void * _Buf1, _In_opt_bytecount_(_Size) const void * _Buf2, _In_ size_t _Size, _In_opt_ _locale_t _Locale);
  50. _Check_return_ int __cdecl memcmp(_In_opt_bytecount_(_Size) const void * _Buf1, _In_opt_bytecount_(_Size) const void * _Buf2, _In_ size_t _Size);
  51. _CRT_INSECURE_DEPRECATE_MEMORY(memcpy_s) void * __cdecl memcpy(_Out_opt_bytecapcount_(_Size) void * _Dst, _In_opt_bytecount_(_Size) const void * _Src, _In_ size_t _Size);
  52. #if __STDC_WANT_SECURE_LIB__
  53. _CRTIMP errno_t __cdecl memcpy_s(_Out_opt_bytecap_post_bytecount_(_DstSize, _MaxCount) void * _Dst, _In_ rsize_t _DstSize, _In_opt_bytecount_(_MaxCount) const void * _Src, _In_ rsize_t _MaxCount);
  54. #if defined(__cplusplus) && _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_MEMORY
  55. extern "C++"
  56. {
  57. #ifndef _CRT_ENABLE_IF_DEFINED
  58. #define _CRT_ENABLE_IF_DEFINED
  59. template<bool _Enable, typename _Ty>
  60. struct _CrtEnableIf;
  61.  
  62. template<typename _Ty>
  63. struct _CrtEnableIf<true, _Ty>
  64. {
  65. typedef _Ty _Type;
  66. };
  67. #endif
  68. template <size_t _Size, typename _DstType>
  69. inline
  70. typename _CrtEnableIf<(_Size > ), void *>::_Type __cdecl memcpy(_DstType (&_Dst)[_Size], _In_opt_bytecount_(_SrcSize) const void *_Src, _In_ size_t _SrcSize) _CRT_SECURE_CPP_NOTHROW
  71. {
  72. return memcpy_s(_Dst, _Size * sizeof(_DstType), _Src, _SrcSize) == ? _Dst : ;
  73. }
  74. }
  75. #endif
  76. #if defined(__cplusplus) && _CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES_MEMORY
  77. extern "C++"
  78. {
  79. template <size_t _Size, typename _DstType>
  80. inline
  81. errno_t __CRTDECL memcpy_s(_DstType (&_Dst)[_Size], _In_opt_bytecount_(_SrcSize) const void * _Src, _In_ rsize_t _SrcSize) _CRT_SECURE_CPP_NOTHROW
  82. {
  83. return memcpy_s(_Dst, _Size * sizeof(_DstType), _Src, _SrcSize);
  84. }
  85. }
  86. #endif
  87. #endif
  88. void * __cdecl memset(_Out_opt_bytecapcount_(_Size) void * _Dst, _In_ int _Val, _In_ size_t _Size);
  89.  
  90. #if !__STDC__
  91. /* Non-ANSI names for compatibility */
  92. _CRT_NONSTDC_DEPRECATE(_memccpy) _CRTIMP void * __cdecl memccpy(_Out_opt_bytecap_(_Size) void * _Dst, _In_opt_bytecount_(_Size) const void * _Src, _In_ int _Val, _In_ size_t _Size);
  93. _Check_return_ _CRT_NONSTDC_DEPRECATE(_memicmp) _CRTIMP int __cdecl memicmp(_In_opt_bytecount_(_Size) const void * _Buf1, _In_opt_bytecount_(_Size) const void * _Buf2, _In_ size_t _Size);
  94. #endif /* __STDC__ */
  95.  
  96. #endif
  97.  
  98. _Check_return_wat_ _CRTIMP_ALTERNATIVE errno_t __cdecl _strset_s(_Inout_z_cap_(_DstSize) char * _Dst, _In_ size_t _DstSize, _In_ int _Value);
  99. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1(errno_t, _strset_s, _Deref_prepost_z_ char, _Dest, _In_ int, _Value)
  100. __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1(char *, __RETURN_POLICY_DST, __EMPTY_DECLSPEC, _strset, _Inout_z_, char, _Dest, _In_ int, _Value)
  101. #if __STDC_WANT_SECURE_LIB__
  102. _Check_return_wat_ _CRTIMP_ALTERNATIVE errno_t __cdecl strcpy_s(_Out_z_cap_(_SizeInBytes) char * _Dst, _In_ rsize_t _SizeInBytes, _In_z_ const char * _Src);
  103. #endif
  104. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1(errno_t, strcpy_s, _Deref_post_z_ char, _Dest, _In_z_ const char *, _Source)
  105. __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1(char *, __RETURN_POLICY_DST, __EMPTY_DECLSPEC, strcpy, _Pre_cap_for_(_Source) _Post_z_, char, _Dest, _In_z_ const char *, _Source)
  106. #if __STDC_WANT_SECURE_LIB__
  107. _Check_return_wat_ _CRTIMP_ALTERNATIVE errno_t __cdecl strcat_s(_Inout_z_cap_(_SizeInBytes) char * _Dst, _In_ rsize_t _SizeInBytes, _In_z_ const char * _Src);
  108. #endif
  109. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1(errno_t, strcat_s, _Deref_prepost_z_ char, _Dest, _In_z_ const char *, _Source)
  110. __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1(char *, __RETURN_POLICY_DST, __EMPTY_DECLSPEC, strcat, _Pre_cap_for_(_Source) _Prepost_z_, char, _Dest, _In_z_ const char *, _Source)
  111. _Check_return_ int __cdecl strcmp(_In_z_ const char * _Str1, _In_z_ const char * _Str2);
  112. _Check_return_ size_t __cdecl strlen(_In_z_ const char * _Str);
  113. _Check_return_ _CRTIMP size_t __cdecl strnlen(_In_z_ const char * _Str, _In_ size_t _MaxCount);
  114. #if __STDC_WANT_SECURE_LIB__ && !defined (__midl)
  115. _Check_return_ static __inline size_t __CRTDECL strnlen_s(_In_z_ const char * _Str, _In_ size_t _MaxCount)
  116. {
  117. return (_Str==) ? : strnlen(_Str, _MaxCount);
  118. }
  119. #endif
  120. #if __STDC_WANT_SECURE_LIB__
  121. _Check_return_wat_ _CRTIMP errno_t __cdecl memmove_s(_Out_opt_bytecap_post_bytecount_(_DstSize,_MaxCount) void * _Dst, _In_ rsize_t _DstSize, _In_opt_bytecount_(_MaxCount) const void * _Src, _In_ rsize_t _MaxCount);
  122. #endif
  123.  
  124. #if defined(_M_IA64)
  125. _CRT_INSECURE_DEPRECATE_MEMORY(memmove_s) void * __cdecl memmove(_Out_opt_bytecapcount_(_Size) void * _Dst, _In_opt_bytecount_(_Size) const void * _Src, _In_ size_t _Size);
  126. #else /* defined (_M_IA64) */
  127. _CRTIMP _CRT_INSECURE_DEPRECATE_MEMORY(memmove_s) void * __cdecl memmove(_Out_opt_bytecapcount_(_Size) void * _Dst, _In_opt_bytecount_(_Size) const void * _Src, _In_ size_t _Size);
  128. #endif /* defined (_M_IA64) */
  129.  
  130. #if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
  131. #pragma push_macro("_strdup")
  132. #undef _strdup
  133. #endif
  134.  
  135. _Check_return_ _CRTIMP char * __cdecl _strdup(_In_opt_z_ const char * _Src);
  136.  
  137. #if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
  138. #pragma pop_macro("_strdup")
  139. #endif
  140.  
  141. _Check_return_ _CRTIMP _CONST_RETURN char * __cdecl strchr(_In_z_ const char * _Str, _In_ int _Val);
  142. _Check_return_ _CRTIMP int __cdecl _stricmp(_In_z_ const char * _Str1, _In_z_ const char * _Str2);
  143. _Check_return_ _CRTIMP int __cdecl _strcmpi(_In_z_ const char * _Str1, _In_z_ const char * _Str2);
  144. _Check_return_ _CRTIMP int __cdecl _stricmp_l(_In_z_ const char * _Str1, _In_z_ const char * _Str2, _In_opt_ _locale_t _Locale);
  145. _Check_return_ _CRTIMP int __cdecl strcoll(_In_z_ const char * _Str1, _In_z_ const char * _Str2);
  146. _Check_return_ _CRTIMP int __cdecl _strcoll_l(_In_z_ const char * _Str1, _In_z_ const char * _Str2, _In_opt_ _locale_t _Locale);
  147. _Check_return_ _CRTIMP int __cdecl _stricoll(_In_z_ const char * _Str1, _In_z_ const char * _Str2);
  148. _Check_return_ _CRTIMP int __cdecl _stricoll_l(_In_z_ const char * _Str1, _In_z_ const char * _Str2, _In_opt_ _locale_t _Locale);
  149. _Check_return_ _CRTIMP int __cdecl _strncoll (_In_z_ const char * _Str1, _In_z_ const char * _Str2, _In_ size_t _MaxCount);
  150. _Check_return_ _CRTIMP int __cdecl _strncoll_l(_In_z_ const char * _Str1, _In_z_ const char * _Str2, _In_ size_t _MaxCount, _In_opt_ _locale_t _Locale);
  151. _Check_return_ _CRTIMP int __cdecl _strnicoll (_In_z_ const char * _Str1, _In_z_ const char * _Str2, _In_ size_t _MaxCount);
  152. _Check_return_ _CRTIMP int __cdecl _strnicoll_l(_In_z_ const char * _Str1, _In_z_ const char * _Str2, _In_ size_t _MaxCount, _In_opt_ _locale_t _Locale);
  153. _Check_return_ _CRTIMP size_t __cdecl strcspn(_In_z_ const char * _Str, _In_z_ const char * _Control);
  154. _Check_return_ _CRT_INSECURE_DEPRECATE(_strerror_s) _CRTIMP char * __cdecl _strerror(_In_opt_z_ const char * _ErrMsg);
  155. _Check_return_wat_ _CRTIMP errno_t __cdecl _strerror_s(_Out_z_cap_(_SizeInBytes) char * _Buf, _In_ size_t _SizeInBytes, _In_opt_z_ const char * _ErrMsg);
  156. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1(errno_t, _strerror_s, char, _Buffer, _In_opt_z_ const char *, _ErrorMessage)
  157. _Check_return_ _CRT_INSECURE_DEPRECATE(strerror_s) _CRTIMP char * __cdecl strerror(_In_ int);
  158. #if __STDC_WANT_SECURE_LIB__
  159. _Check_return_wat_ _CRTIMP errno_t __cdecl strerror_s(_Out_z_cap_(_SizeInBytes) char * _Buf, _In_ size_t _SizeInBytes, _In_ int _ErrNum);
  160. #endif
  161. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1(errno_t, strerror_s, char, _Buffer, _In_ int, _ErrorMessage)
  162. _Check_return_wat_ _CRTIMP errno_t __cdecl _strlwr_s(_Inout_z_cap_(_Size) char * _Str, _In_ size_t _Size);
  163. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_0(errno_t, _strlwr_s, _Deref_prepost_z_ char, _String)
  164. __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_0(char *, __RETURN_POLICY_DST, _CRTIMP, _strlwr, _Inout_z_, char, _String)
  165. _Check_return_wat_ _CRTIMP errno_t __cdecl _strlwr_s_l(_Inout_z_cap_(_Size) char * _Str, _In_ size_t _Size, _In_opt_ _locale_t _Locale);
  166. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1(errno_t, _strlwr_s_l, _Deref_prepost_z_ char, _String, _In_opt_ _locale_t, _Locale)
  167. __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1_EX(char *, __RETURN_POLICY_DST, _CRTIMP, _strlwr_l, _strlwr_s_l, _Deref_inout_z_cap_c_(_Size) char, _Inout_z_, char, _String, _In_opt_ _locale_t, _Locale)
  168. #if __STDC_WANT_SECURE_LIB__
  169. _Check_return_wat_ _CRTIMP_ALTERNATIVE errno_t __cdecl strncat_s(_Inout_z_cap_(_SizeInBytes) char * _Dst, _In_ rsize_t _SizeInBytes, _In_z_ const char * _Src, _In_ rsize_t _MaxCount);
  170. #endif
  171. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_2(errno_t, strncat_s, _Deref_prepost_z_ char, _Dest, _In_z_ const char *, _Source, _In_ size_t, _Count)
  172. #pragma warning(push)
  173. #pragma warning(disable:6059)
  174. /* prefast noise VSW 489802 */
  175. __DEFINE_CPP_OVERLOAD_STANDARD_NFUNC_0_2_EX(char *, __RETURN_POLICY_DST, _CRTIMP, strncat, strncat_s, _Deref_inout_z_cap_c_(_Size) char, _Inout_z_cap_(_Count), char, _Dest, _In_z_ const char *, _Source, _In_ size_t, _Count)
  176. #pragma warning(pop)
  177. #if defined(_M_IA64)
  178. _Check_return_ int __cdecl strncmp(_In_z_ const char * _Str1, _In_z_ const char * _Str2, _In_ size_t _MaxCount);
  179. #else
  180. _Check_return_ _CRTIMP int __cdecl strncmp(_In_z_ const char * _Str1, _In_z_ const char * _Str2, _In_ size_t _MaxCount);
  181. #endif
  182. _Check_return_ _CRTIMP int __cdecl _strnicmp(_In_z_ const char * _Str1, _In_z_ const char * _Str2, _In_ size_t _MaxCount);
  183. _Check_return_ _CRTIMP int __cdecl _strnicmp_l(_In_z_ const char * _Str1, _In_z_ const char * _Str2, _In_ size_t _MaxCount, _In_opt_ _locale_t _Locale);
  184. #if __STDC_WANT_SECURE_LIB__
  185. _Check_return_wat_ _CRTIMP_ALTERNATIVE errno_t __cdecl strncpy_s(_Out_z_cap_(_SizeInBytes) char * _Dst, _In_ rsize_t _SizeInBytes, _In_z_ const char * _Src, _In_ rsize_t _MaxCount);
  186. #endif
  187. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_2(errno_t, strncpy_s, char, _Dest, _In_z_ const char *, _Source, _In_ size_t, _Count)
  188. __DEFINE_CPP_OVERLOAD_STANDARD_NFUNC_0_2_EX(char *, __RETURN_POLICY_DST, _CRTIMP_NOIA64, strncpy, strncpy_s, _Deref_out_z_cap_c_(_Size) char, _Out_cap_(_Count) _Post_maybez_, char, _Dest, _In_z_ const char *, _Source, _In_ size_t, _Count)
  189. _Check_return_wat_ _CRTIMP_ALTERNATIVE errno_t __cdecl _strnset_s(_Inout_z_cap_(_SizeInBytes) char * _Str, _In_ size_t _SizeInBytes, _In_ int _Val, _In_ size_t _MaxCount);
  190. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_2(errno_t, _strnset_s, _Deref_prepost_z_ char, _Dest, _In_ int, _Val, _In_ size_t, _Count)
  191. __DEFINE_CPP_OVERLOAD_STANDARD_NFUNC_0_2_EX(char *, __RETURN_POLICY_DST, _CRTIMP, _strnset, _strnset_s, _Deref_inout_z_cap_c_(_Size) char, _Inout_z_cap_(_Count), char, _Dest, _In_ int, _Val, _In_ size_t, _Count)
  192. _Check_return_ _CRTIMP _CONST_RETURN char * __cdecl strpbrk(_In_z_ const char * _Str, _In_z_ const char * _Control);
  193. _Check_return_ _CRTIMP _CONST_RETURN char * __cdecl strrchr(_In_z_ const char * _Str, _In_ int _Ch);
  194. _CRTIMP char * __cdecl _strrev(_Inout_z_ char * _Str);
  195. _Check_return_ _CRTIMP size_t __cdecl strspn(_In_z_ const char * _Str, _In_z_ const char * _Control);
  196. _Check_return_ _CRTIMP _CONST_RETURN char * __cdecl strstr(_In_z_ const char * _Str, _In_z_ const char * _SubStr);
  197. _Check_return_ _CRT_INSECURE_DEPRECATE(strtok_s) _CRTIMP char * __cdecl strtok(_Inout_opt_z_ char * _Str, _In_z_ const char * _Delim);
  198. #if __STDC_WANT_SECURE_LIB__
  199. _Check_return_ _CRTIMP_ALTERNATIVE char * __cdecl strtok_s(_Inout_opt_z_ char * _Str, _In_z_ const char * _Delim, _Inout_ _Deref_prepost_opt_z_ char ** _Context);
  200. #endif
  201. _Check_return_wat_ _CRTIMP errno_t __cdecl _strupr_s(_Inout_z_cap_(_Size) char * _Str, _In_ size_t _Size);
  202. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_0(errno_t, _strupr_s, _Deref_prepost_z_ char, _String)
  203. __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_0(char *, __RETURN_POLICY_DST, _CRTIMP, _strupr, _Inout_z_, char, _String)
  204. _Check_return_wat_ _CRTIMP errno_t __cdecl _strupr_s_l(_Inout_z_cap_(_Size) char * _Str, _In_ size_t _Size, _locale_t _Locale);
  205. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1(errno_t, _strupr_s_l, _Deref_prepost_z_ char, _String, _locale_t, _Locale)
  206. __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1_EX(char *, __RETURN_POLICY_DST, _CRTIMP, _strupr_l, _strupr_s_l, _Deref_inout_z_cap_c_(_Size) char, _Inout_z_, char, _String, _In_opt_ _locale_t, _Locale)
  207. _Check_return_opt_ _CRTIMP size_t __cdecl strxfrm (_Out_opt_cap_(_MaxCount) _Post_maybez_ char * _Dst, _In_z_ const char * _Src, _In_ size_t _MaxCount);
  208. _Check_return_opt_ _CRTIMP size_t __cdecl _strxfrm_l(_Out_opt_cap_(_MaxCount) _Post_maybez_ char * _Dst, _In_z_ const char * _Src, _In_ size_t _MaxCount, _In_opt_ _locale_t _Locale);
  209.  
  210. #ifdef __cplusplus
  211. extern "C++" {
  212. #ifndef _CPP_NARROW_INLINES_DEFINED
  213. #define _CPP_NARROW_INLINES_DEFINED
  214. _Check_return_ inline char * __CRTDECL strchr(_In_z_ char * _Str, _In_ int _Ch)
  215. { return (char*)strchr((const char*)_Str, _Ch); }
  216. _Check_return_ inline char * __CRTDECL strpbrk(_In_z_ char * _Str, _In_z_ const char * _Control)
  217. { return (char*)strpbrk((const char*)_Str, _Control); }
  218. _Check_return_ inline char * __CRTDECL strrchr(_In_z_ char * _Str, _In_ int _Ch)
  219. { return (char*)strrchr((const char*)_Str, _Ch); }
  220. _Check_return_ inline char * __CRTDECL strstr(_In_z_ char * _Str, _In_z_ const char * _SubStr)
  221. { return (char*)strstr((const char*)_Str, _SubStr); }
  222. #endif
  223. #ifndef _CPP_MEMCHR_DEFINED
  224. #define _CPP_MEMCHR_DEFINED
  225. _Check_return_ inline void * __CRTDECL memchr(_In_opt_bytecount_(_N) void * _Pv, _In_ int _C, _In_ size_t _N)
  226. { return (void*)memchr((const void*)_Pv, _C, _N); }
  227. #endif
  228. }
  229. #endif
  230.  
  231. #if !__STDC__
  232.  
  233. #if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
  234. #pragma push_macro("strdup")
  235. #undef strdup
  236. #endif
  237.  
  238. _Check_return_ _CRT_NONSTDC_DEPRECATE(_strdup) _CRTIMP char * __cdecl strdup(_In_opt_z_ const char * _Src);
  239.  
  240. #if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
  241. #pragma pop_macro("strdup")
  242. #endif
  243.  
  244. /* prototypes for oldnames.lib functions */
  245. _Check_return_ _CRT_NONSTDC_DEPRECATE(_strcmpi) _CRTIMP int __cdecl strcmpi(_In_z_ const char * _Str1, _In_z_ const char * _Str2);
  246. _Check_return_ _CRT_NONSTDC_DEPRECATE(_stricmp) _CRTIMP int __cdecl stricmp(_In_z_ const char * _Str1, _In_z_ const char * _Str2);
  247. _CRT_NONSTDC_DEPRECATE(_strlwr) _CRTIMP char * __cdecl strlwr(_Inout_z_ char * _Str);
  248. _Check_return_ _CRT_NONSTDC_DEPRECATE(_strnicmp) _CRTIMP int __cdecl strnicmp(_In_z_ const char * _Str1, _In_z_ const char * _Str, _In_ size_t _MaxCount);
  249. _CRT_NONSTDC_DEPRECATE(_strnset) _CRTIMP char * __cdecl strnset(_Inout_z_cap_(_MaxCount) char * _Str, _In_ int _Val, _In_ size_t _MaxCount);
  250. _CRT_NONSTDC_DEPRECATE(_strrev) _CRTIMP char * __cdecl strrev(_Inout_z_ char * _Str);
  251. _CRT_NONSTDC_DEPRECATE(_strset) char * __cdecl strset(_Inout_z_ char * _Str, _In_ int _Val);
  252. _CRT_NONSTDC_DEPRECATE(_strupr) _CRTIMP char * __cdecl strupr(_Inout_z_ char * _Str);
  253.  
  254. #endif /* !__STDC__ */
  255.  
  256. #ifndef _WSTRING_DEFINED
  257.  
  258. /* wide function prototypes, also declared in wchar.h */
  259.  
  260. #if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
  261. #pragma push_macro("_wcsdup")
  262. #undef _wcsdup
  263. #endif
  264.  
  265. _Check_return_ _CRTIMP wchar_t * __cdecl _wcsdup(_In_z_ const wchar_t * _Str);
  266.  
  267. #if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
  268. #pragma pop_macro("_wcsdup")
  269. #endif
  270.  
  271. #if __STDC_WANT_SECURE_LIB__
  272. _Check_return_wat_ _CRTIMP_ALTERNATIVE errno_t __cdecl wcscat_s(_Inout_z_cap_(_SizeInWords) wchar_t * _Dst, _In_ rsize_t _SizeInWords, _In_z_ const wchar_t * _Src);
  273. #endif
  274. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1(errno_t, wcscat_s, _Deref_prepost_z_ wchar_t, _Dest, _In_z_ const wchar_t *, _Source)
  275. __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1(wchar_t *, __RETURN_POLICY_DST, _CRTIMP, wcscat, _Pre_cap_for_(_Source) _Prepost_z_, wchar_t, _Dest, _In_z_ const wchar_t *, _Source)
  276. _Check_return_ _CRTIMP _CONST_RETURN wchar_t * __cdecl wcschr(_In_z_ const wchar_t * _Str, wchar_t _Ch);
  277. _Check_return_ _CRTIMP int __cdecl wcscmp(_In_z_ const wchar_t * _Str1, _In_z_ const wchar_t * _Str2);
  278. #if __STDC_WANT_SECURE_LIB__
  279. _Check_return_wat_ _CRTIMP_ALTERNATIVE errno_t __cdecl wcscpy_s(_Out_z_cap_(_SizeInWords) wchar_t * _Dst, _In_ rsize_t _SizeInWords, _In_z_ const wchar_t * _Src);
  280. #endif
  281. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1(errno_t, wcscpy_s, wchar_t, _Dest, _In_z_ const wchar_t *, _Source)
  282. __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1(wchar_t *, __RETURN_POLICY_DST, _CRTIMP, wcscpy, _Pre_cap_for_(_Source) _Post_z_, wchar_t, _Dest, _In_z_ const wchar_t *, _Source)
  283. _Check_return_ _CRTIMP size_t __cdecl wcscspn(_In_z_ const wchar_t * _Str, _In_z_ const wchar_t * _Control);
  284. _Check_return_ _CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t * _Str);
  285. _Check_return_ _CRTIMP size_t __cdecl wcsnlen(_In_z_ const wchar_t * _Src, _In_ size_t _MaxCount);
  286. #if __STDC_WANT_SECURE_LIB__ && !defined (__midl)
  287. _Check_return_ static __inline size_t __CRTDECL wcsnlen_s(_In_z_ const wchar_t * _Src, _In_ size_t _MaxCount)
  288. {
  289. return (_Src == NULL) ? : wcsnlen(_Src, _MaxCount);
  290. }
  291. #endif
  292. #if __STDC_WANT_SECURE_LIB__
  293. _Check_return_wat_ _CRTIMP_ALTERNATIVE errno_t __cdecl wcsncat_s(_Inout_z_cap_(_SizeInWords) wchar_t * _Dst, _In_ rsize_t _SizeInWords, _In_z_ const wchar_t * _Src, _In_ rsize_t _MaxCount);
  294. #endif
  295. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_2(errno_t, wcsncat_s, _Deref_prepost_z_ wchar_t, _Dest, _In_z_ const wchar_t *, _Source, _In_ size_t, _Count)
  296. #pragma warning(push)
  297. #pragma warning(disable:6059)
  298. __DEFINE_CPP_OVERLOAD_STANDARD_NFUNC_0_2_EX(wchar_t *, __RETURN_POLICY_DST, _CRTIMP, wcsncat, wcsncat_s, _Deref_inout_z_cap_c_(_Size) wchar_t, _Inout_z_cap_(_Count), wchar_t, _Dest, _In_z_ const wchar_t *, _Source, _In_ size_t, _Count)
  299. #pragma warning(pop)
  300. _Check_return_ _CRTIMP int __cdecl wcsncmp(_In_z_ const wchar_t * _Str1, _In_z_ const wchar_t * _Str2, _In_ size_t _MaxCount);
  301. #if __STDC_WANT_SECURE_LIB__
  302. _Check_return_wat_ _CRTIMP_ALTERNATIVE errno_t __cdecl wcsncpy_s(_Out_z_cap_(_SizeInWords) wchar_t * _Dst, _In_ rsize_t _SizeInWords, _In_z_ const wchar_t * _Src, _In_ rsize_t _MaxCount);
  303. #endif
  304. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_2(errno_t, wcsncpy_s, wchar_t, _Dest, _In_z_ const wchar_t *, _Source, _In_ size_t, _Count)
  305. __DEFINE_CPP_OVERLOAD_STANDARD_NFUNC_0_2_EX(wchar_t *, __RETURN_POLICY_DST, _CRTIMP, wcsncpy, wcsncpy_s, _Pre_notnull_ _Post_maybez_ wchar_t, _Out_cap_(_Count) _Post_maybez_, wchar_t, _Dest, _In_z_ const wchar_t *, _Source, _In_ size_t, _Count)
  306. _Check_return_ _CRTIMP _CONST_RETURN wchar_t * __cdecl wcspbrk(_In_z_ const wchar_t * _Str, _In_z_ const wchar_t * _Control);
  307. _Check_return_ _CRTIMP _CONST_RETURN wchar_t * __cdecl wcsrchr(_In_z_ const wchar_t * _Str, _In_ wchar_t _Ch);
  308. _Check_return_ _CRTIMP size_t __cdecl wcsspn(_In_z_ const wchar_t * _Str, _In_z_ const wchar_t * _Control);
  309. _Check_return_ _CRTIMP _CONST_RETURN wchar_t * __cdecl wcsstr(_In_z_ const wchar_t * _Str, _In_z_ const wchar_t * _SubStr);
  310. _Check_return_ _CRT_INSECURE_DEPRECATE(wcstok_s) _CRTIMP wchar_t * __cdecl wcstok(_Inout_opt_z_ wchar_t * _Str, _In_z_ const wchar_t * _Delim);
  311. #if __STDC_WANT_SECURE_LIB__
  312. _Check_return_ _CRTIMP_ALTERNATIVE wchar_t * __cdecl wcstok_s(_Inout_opt_z_ wchar_t * _Str, _In_z_ const wchar_t * _Delim, _Inout_ _Deref_prepost_opt_z_ wchar_t ** _Context);
  313. #endif
  314. _Check_return_ _CRT_INSECURE_DEPRECATE(_wcserror_s) _CRTIMP wchar_t * __cdecl _wcserror(_In_ int _ErrNum);
  315. _Check_return_wat_ _CRTIMP errno_t __cdecl _wcserror_s(_Out_opt_z_cap_(_SizeInWords) wchar_t * _Buf, _In_ size_t _SizeInWords, _In_ int _ErrNum);
  316. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1(errno_t, _wcserror_s, wchar_t, _Buffer, _In_ int, _Error)
  317. _Check_return_ _CRT_INSECURE_DEPRECATE(__wcserror_s) _CRTIMP wchar_t * __cdecl __wcserror(_In_opt_z_ const wchar_t * _Str);
  318. _Check_return_wat_ _CRTIMP errno_t __cdecl __wcserror_s(_Out_opt_z_cap_(_SizeInWords) wchar_t * _Buffer, _In_ size_t _SizeInWords, _In_z_ const wchar_t * _ErrMsg);
  319. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1(errno_t, __wcserror_s, wchar_t, _Buffer, _In_z_ const wchar_t *, _ErrorMessage)
  320.  
  321. _Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t * _Str1, _In_z_ const wchar_t * _Str2);
  322. _Check_return_ _CRTIMP int __cdecl _wcsicmp_l(_In_z_ const wchar_t * _Str1, _In_z_ const wchar_t * _Str2, _In_opt_ _locale_t _Locale);
  323. _Check_return_ _CRTIMP int __cdecl _wcsnicmp(_In_z_ const wchar_t * _Str1, _In_z_ const wchar_t * _Str2, _In_ size_t _MaxCount);
  324. _Check_return_ _CRTIMP int __cdecl _wcsnicmp_l(_In_z_ const wchar_t * _Str1, _In_z_ const wchar_t * _Str2, _In_ size_t _MaxCount, _In_opt_ _locale_t _Locale);
  325. _Check_return_wat_ _CRTIMP_ALTERNATIVE errno_t __cdecl _wcsnset_s(_Inout_z_cap_(_SizeInWords) wchar_t * _Dst, _In_ size_t _SizeInWords, _In_ wchar_t _Val, _In_ size_t _MaxCount);
  326. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_2(errno_t, _wcsnset_s, _Deref_prepost_z_ wchar_t, _Dst, wchar_t, _Val, _In_ size_t, _MaxCount)
  327. __DEFINE_CPP_OVERLOAD_STANDARD_NFUNC_0_2_EX(wchar_t *, __RETURN_POLICY_DST, _CRTIMP, _wcsnset, _wcsnset_s, _Deref_inout_z_cap_c_(_Size) wchar_t, _Inout_z_cap_(_MaxCount), wchar_t, _Str, wchar_t, _Val, _In_ size_t, _MaxCount)
  328. _CRTIMP wchar_t * __cdecl _wcsrev(_Inout_z_ wchar_t * _Str);
  329. _Check_return_wat_ _CRTIMP_ALTERNATIVE errno_t __cdecl _wcsset_s(_Inout_z_cap_(_SizeInWords) wchar_t * _Dst, _In_ size_t _SizeInWords, _In_ wchar_t _Value);
  330. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1(errno_t, _wcsset_s, _Deref_prepost_z_ wchar_t, _Str, wchar_t, _Val)
  331. __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1_EX(wchar_t *, __RETURN_POLICY_DST, _CRTIMP, _wcsset, _wcsset_s, _Deref_inout_z_cap_c_(_Size) wchar_t, _Inout_z_, wchar_t, _Str, wchar_t, _Val)
  332.  
  333. _Check_return_wat_ _CRTIMP errno_t __cdecl _wcslwr_s(_Inout_z_cap_(_SizeInWords) wchar_t * _Str, _In_ size_t _SizeInWords);
  334. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_0(errno_t, _wcslwr_s, _Deref_prepost_z_ wchar_t, _String)
  335. __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_0(wchar_t *, __RETURN_POLICY_DST, _CRTIMP, _wcslwr, _Inout_z_, wchar_t, _String)
  336. _Check_return_wat_ _CRTIMP errno_t __cdecl _wcslwr_s_l(_Inout_z_cap_(_SizeInWords) wchar_t * _Str, _In_ size_t _SizeInWords, _In_opt_ _locale_t _Locale);
  337. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1(errno_t, _wcslwr_s_l, _Deref_prepost_z_ wchar_t, _String, _In_opt_ _locale_t, _Locale)
  338. __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1_EX(wchar_t *, __RETURN_POLICY_DST, _CRTIMP, _wcslwr_l, _wcslwr_s_l, _Deref_inout_z_cap_c_(_Size) wchar_t, _Inout_z_, wchar_t, _String, _In_opt_ _locale_t, _Locale)
  339. _Check_return_wat_ _CRTIMP errno_t __cdecl _wcsupr_s(_Inout_z_cap_(_Size) wchar_t * _Str, _In_ size_t _Size);
  340. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_0(errno_t, _wcsupr_s, _Deref_prepost_z_ wchar_t, _String)
  341. __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_0(wchar_t *, __RETURN_POLICY_DST, _CRTIMP, _wcsupr, _Inout_z_, wchar_t, _String)
  342. _Check_return_wat_ _CRTIMP errno_t __cdecl _wcsupr_s_l(_Inout_z_cap_(_Size) wchar_t * _Str, _In_ size_t _Size, _In_opt_ _locale_t _Locale);
  343. __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1(errno_t, _wcsupr_s_l, _Deref_prepost_z_ wchar_t, _String, _In_opt_ _locale_t, _Locale)
  344. __DEFINE_CPP_OVERLOAD_STANDARD_FUNC_0_1_EX(wchar_t *, __RETURN_POLICY_DST, _CRTIMP, _wcsupr_l, _wcsupr_s_l, _Deref_inout_z_cap_c_(_Size) wchar_t, _Inout_z_, wchar_t, _String, _In_opt_ _locale_t, _Locale)
  345. _Check_return_opt_ _CRTIMP size_t __cdecl wcsxfrm(_Out_opt_cap_(_MaxCount) _Post_maybez_ wchar_t * _Dst, _In_z_ const wchar_t * _Src, _In_ size_t _MaxCount);
  346. _Check_return_opt_ _CRTIMP size_t __cdecl _wcsxfrm_l(_Out_opt_cap_(_MaxCount) _Post_maybez_ wchar_t * _Dst, _In_z_ const wchar_t *_Src, _In_ size_t _MaxCount, _In_opt_ _locale_t _Locale);
  347. _Check_return_ _CRTIMP int __cdecl wcscoll(_In_z_ const wchar_t * _Str1, _In_z_ const wchar_t * _Str2);
  348. _Check_return_ _CRTIMP int __cdecl _wcscoll_l(_In_z_ const wchar_t * _Str1, _In_z_ const wchar_t * _Str2, _In_opt_ _locale_t _Locale);
  349. _Check_return_ _CRTIMP int __cdecl _wcsicoll(_In_z_ const wchar_t * _Str1, _In_z_ const wchar_t * _Str2);
  350. _Check_return_ _CRTIMP int __cdecl _wcsicoll_l(_In_z_ const wchar_t * _Str1, _In_z_ const wchar_t *_Str2, _In_opt_ _locale_t _Locale);
  351. _Check_return_ _CRTIMP int __cdecl _wcsncoll(_In_z_ const wchar_t * _Str1, _In_z_ const wchar_t * _Str2, _In_ size_t _MaxCount);
  352. _Check_return_ _CRTIMP int __cdecl _wcsncoll_l(_In_z_ const wchar_t * _Str1, _In_z_ const wchar_t * _Str2, _In_ size_t _MaxCount, _In_opt_ _locale_t _Locale);
  353. _Check_return_ _CRTIMP int __cdecl _wcsnicoll(_In_z_ const wchar_t * _Str1, _In_z_ const wchar_t * _Str2, _In_ size_t _MaxCount);
  354. _Check_return_ _CRTIMP int __cdecl _wcsnicoll_l(_In_z_ const wchar_t * _Str1, _In_z_ const wchar_t * _Str2, _In_ size_t _MaxCount, _In_opt_ _locale_t _Locale);
  355.  
  356. #ifdef __cplusplus
  357. #ifndef _CPP_WIDE_INLINES_DEFINED
  358. #define _CPP_WIDE_INLINES_DEFINED
  359. extern "C++" {
  360. _Check_return_ inline wchar_t * __CRTDECL wcschr(_In_z_ wchar_t *_Str, wchar_t _Ch)
  361. {return ((wchar_t *)wcschr((const wchar_t *)_Str, _Ch)); }
  362. _Check_return_ inline wchar_t * __CRTDECL wcspbrk(_In_z_ wchar_t *_Str, _In_z_ const wchar_t *_Control)
  363. {return ((wchar_t *)wcspbrk((const wchar_t *)_Str, _Control)); }
  364. _Check_return_ inline wchar_t * __CRTDECL wcsrchr(_In_z_ wchar_t *_Str, _In_ wchar_t _Ch)
  365. {return ((wchar_t *)wcsrchr((const wchar_t *)_Str, _Ch)); }
  366. _Check_return_ inline wchar_t * __CRTDECL wcsstr(_In_z_ wchar_t *_Str, _In_z_ const wchar_t *_SubStr)
  367. {return ((wchar_t *)wcsstr((const wchar_t *)_Str, _SubStr)); }
  368. }
  369. #endif
  370. #endif
  371.  
  372. #if !__STDC__
  373.  
  374. #if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
  375. #pragma push_macro("wcsdup")
  376. #undef wcsdup
  377. #endif
  378.  
  379. _Check_return_ _CRT_NONSTDC_DEPRECATE(_wcsdup) _CRTIMP wchar_t * __cdecl wcsdup(_In_z_ const wchar_t * _Str);
  380.  
  381. #if defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
  382. #pragma pop_macro("wcsdup")
  383. #endif
  384.  
  385. /* old names */
  386. #define wcswcs wcsstr
  387.  
  388. /* prototypes for oldnames.lib functions */
  389. _Check_return_ _CRT_NONSTDC_DEPRECATE(_wcsicmp) _CRTIMP int __cdecl wcsicmp(_In_z_ const wchar_t * _Str1, _In_z_ const wchar_t * _Str2);
  390. _Check_return_ _CRT_NONSTDC_DEPRECATE(_wcsnicmp) _CRTIMP int __cdecl wcsnicmp(_In_z_ const wchar_t * _Str1, _In_z_ const wchar_t * _Str2, _In_ size_t _MaxCount);
  391. _CRT_NONSTDC_DEPRECATE(_wcsnset) _CRTIMP wchar_t * __cdecl wcsnset(_Inout_z_cap_(_MaxCount) wchar_t * _Str, _In_ wchar_t _Val, _In_ size_t _MaxCount);
  392. _CRT_NONSTDC_DEPRECATE(_wcsrev) _CRTIMP wchar_t * __cdecl wcsrev(_Inout_z_ wchar_t * _Str);
  393. _CRT_NONSTDC_DEPRECATE(_wcsset) _CRTIMP wchar_t * __cdecl wcsset(_Inout_z_ wchar_t * _Str, wchar_t _Val);
  394. _CRT_NONSTDC_DEPRECATE(_wcslwr) _CRTIMP wchar_t * __cdecl wcslwr(_Inout_z_ wchar_t * _Str);
  395. _CRT_NONSTDC_DEPRECATE(_wcsupr) _CRTIMP wchar_t * __cdecl wcsupr(_Inout_z_ wchar_t * _Str);
  396. _Check_return_ _CRT_NONSTDC_DEPRECATE(_wcsicoll) _CRTIMP int __cdecl wcsicoll(_In_z_ const wchar_t * _Str1, _In_z_ const wchar_t * _Str2);
  397.  
  398. #endif /* !__STDC__ */
  399.  
  400. #define _WSTRING_DEFINED
  401. #endif
  402.  
  403. #ifdef __cplusplus
  404. }
  405. #endif
  406.  
  407. #endif /* _INC_STRING */

在Vc\include下面倒是有一个string的cpp文件,但是没有string操作函数的代码,那么strcpy,strcat源码到底在哪里?

D:\Program Files\visual studio\VC\crt\src 目录下面

这个目录下面包含所有C语言函数的源代码

大多数函数的名字都是以名字命名的.c文件里,但是strcpy并没有strcpy.c文件,它的源码在strcat.c里面:

strcat.c内容如下:

  1. /***
  2. *strcat.c - contains strcat() and strcpy()
  3.  
  4. *
  5. *Purpose:
  6. * Strcpy() copies one string onto another.
  7. *
  8. * Strcat() concatenates (appends) a copy of the source string to the
  9. * end of the destination string, returning the destination string.
  10. *
  11. *******************************************************************************/
  12.  
  13. #include <cruntime.h>
  14. #include <string.h>
  15.  
  16. #ifndef _MBSCAT
  17. #pragma function(strcat,strcpy)
  18. #endif /* _MBSCAT */
  19.  
  20. /***
  21. *char *strcat(dst, src) - concatenate (append) one string to another
  22. *
  23. *Purpose:
  24. * Concatenates src onto the end of dest. Assumes enough
  25. * space in dest.
  26. *
  27. *Entry:
  28. * char *dst - string to which "src" is to be appended
  29. * const char *src - string to be appended to the end of "dst"
  30. *
  31. *Exit:
  32. * The address of "dst"
  33. *
  34. *Exceptions:
  35. *
  36. *******************************************************************************/
  37.  
  38. char * __cdecl strcat (
  39. char * dst,
  40. const char * src
  41. )
  42. {
  43. char * cp = dst;
  44.  
  45. while( *cp )
  46. cp++; /* find end of dst */
    /*不能写成while(*cp++)/ 因为这样的话为*cp为0时还会+1,导致cp执向了\0的后一个。
  47.  
  48. while( *cp++ = *src++ ) ; /* Copy src to end of dst */
  49.  
  50. return( dst ); /* return dst */
  51.  
  52. }
  53.  
  54. /***
  55. *char *strcpy(dst, src) - copy one string over another
  56. *
  57. *Purpose:
  58. * Copies the string src into the spot specified by
  59. * dest; assumes enough room.
  60. *
  61. *Entry:
  62. * char * dst - string over which "src" is to be copied
  63. * const char * src - string to be copied over "dst"
  64. *
  65. *Exit:
  66. * The address of "dst"
  67. *
  68. *Exceptions:
  69. *******************************************************************************/
  70.  
  71. char * __cdecl strcpy(char * dst, const char * src)
  72. {
  73. char * cp = dst;
  74.  
  75. while( *cp++ = *src++ )
  76. ; /* Copy src over dst */
  77.  
  78. return( dst );
  79. }

strcpy另一种实现策略:

林锐《高质量C++/C编程指南》

  1. #include <assert.h>
  2. char *strcpy(char *strDest, const char *strSrc)
  3. {
  4.  
  5. assert((strDest!=NULL) && (strSrc !=NULL)); // 2分
  6.  
  7. char *address = strDest; // 2分
  8.  
  9. while( (*strDest++ = * strSrc++) != ‘/’ ) // 2分
  10.  
  11. ;
  12.  
  13. return address ; // 2分
  14.  
  15. }

附:

strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?

答:为了实现链式表达式。 // 2分
例如 int length = strlen( strcpy( strDest, “hello world”) );

个人觉得

  1. (*strDest++ = * strSrc++) != ‘/0 不能没有必要判断是否=0,因为但char内容为字符串结束符时ascii0dst=src; dst=0;
    while 判断dst是否等于0,等于0就退出了。
  2.  
  3. strcmp怎么写,最开始我这么写的:
  1. int strcmp(char *src,char *dst)
  2. {
  3. char *p=src,*q=dst;
  4. while( (*p!='\0') && (*q!='\0'))
  5. {
  6. if(*p==*q)
  7. {
  8. p++;
  9. q++;
  10. }
  11. else if(*p<*q)
  12. {
  13. return -;
  14. }
  15. else
  16. {
  17. return ;
  18. }
  19. }
  20. return ;
  21. }

表面上看没有问题,但是有很大的问题,但2个长度不等时,退出循环还是输出了0,。如de和def比较还是0.

可以把return改下,判断*P和*q是否后面还有。显然太麻烦了。

函数源码:

  1. /***
  2. *strcmp - compare two strings, returning less than, equal to, or greater than
  3. *
  4. *Purpose:
  5. * STRCMP compares two strings and returns an integer
  6. * to indicate whether the first is less than the second, the two are
  7. * equal, or whether the first is greater than the second.
  8. *
  9. * Comparison is done byte by byte on an UNSIGNED basis, which is to
  10. * say that Null (0) is less than any other character (1-255).
  11. *
  12. *Entry:
  13. * const char * src - string for left-hand side of comparison
  14. * const char * dst - string for right-hand side of comparison
  15. *
  16. *Exit:
  17. * returns -1 if src < dst
  18. * returns 0 if src == dst
  19. * returns +1 if src > dst
  20. *
  21. *Exceptions:
  22. *
  23. *******************************************************************************/
  24.  
  25. int __cdecl strcmp (
  26. const char * src,
  27. const char * dst
  28. )
  29. {
  30. int ret = ;
  31.  
  32. while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
  33. ++src, ++dst;
  34.  
  35. if ( ret < )
  36. ret = - ;
  37. else if ( ret > )
  38. ret = ;
  39.  
  40. return( ret );
  41. }

刚开始对

  1. while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
  2. ++src, ++dst;
    不是很理解。
    如果是dedef比较,*src没有为0,还是在继续比较吗?
    刚开始我们认为是在继续比较,其实没有比较了。因为。
    ! ret
    ret=0才继续比较。
    ret=0-'f'=负数。
    !负数 为假。 0为假,非0为真。
    !0为真
    这里取非很巧妙。没有
  1. ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *src && *dst)这种。
    strcmp算法的可以有多种,不过我觉的可以把这么多算法分为两种:一种是利用减法运算判断结果,另一种是利用比较运算==得出结果。上面的就是用减法。判断。
    为什么要把src转成:
  1. unsigned char *

这个函数要注意的几点:

(1)使用*(unsigned char *)str1而不是用*str1。这是因为传入的参数为有符号数,

有符号字符值的范围是-128-127,无符号字符值的范围是0-255,而字符串的ASCII没有负值,若不转化为无符号数,在减法实现时出现错误。

例如:str的值为1,str2的值为255。

作为无符号数计算时ret=-254,结果为负值,正确。

作为有符号数计算时ret=-2,结果为正值,错误。

ascii char范围为0-127,为什么上面说的255.怎么才能打印255的字符。

在cmd下,按alt+255 回车,就可以打印ÿ。这个不是ascii字符。

我们把这个字符粘贴到vs2010文件中,会提示当前编码文件为ascii格式,不能存储当前的字符,是否转换为unicode,转换为unicode就可以存储这个字符了。

char *c="dÿ";
printf("%s\n",c);

运行控制台会输出:

d?

?说明第二个字符不可打印。

(2)while循环中(ret=*(unsigned char *)str1 - *(unsigned char *)str2)&& *str1,最后的str1也可以换成str2,因为前面已经做了相减,无论哪个先为'\0'都会退出。

(3)这个函数没有判断参数为NULL时的情况,所以当传入NULL时程序会崩溃。

  1. 可以看到我们传递的参数有const,这会为面试加分。

用比较实现

  1. int strcmp(const char *str1,const char * str2)
  2.  
  3. {
  4.  
  5.   while((*str1)&&(*str1 == *str2))
  6.  
  7.   {
  8.     str1++;
  9.     str2++;
  10.   }
  11.  
  12.   if(*(unsiged char *)str1 > *(unsiged char *)str2)
  13.  
  14.   {
  15.     return ;
  16.  
  17.   }
  18.  
  19.   else if(*(unsiged char *)str1 < *(unsiged char *)str2)
  20.  
  21.   {
  22.  
  23.     return -;
  24.  
  25.   }
  26.  
  27.   else
  28.  
  29.   {
  30.     return ;
  31.   }
  32.  
  33. }

注意:把while循环简写成while((*str1)&&(*str1++ == *str2++))

当str1为abcd, str2为abfd时,由于判断到第三个字符时while推出,而str指针又加了1,str都指向第四个字符输出结果为0,显然 这是错误的。

参考了:http://www.cnblogs.com/kaifublog/archive/2012/09/23/2699017.html

  1. strlen
  1. int strlen(const char *str)
  2. {
  3. int len = ;
  4. assert(str != NULL);
  5. while(*str++)
  6. {
  7. len++;
  8. }
  9. return len;
  10. }

我刚开始写的是:

  1. int strlen2(const char *src)
  2. {
  3. const char *p=src;
  4. int i=;
  5. while(*p++)
  6. i++;
  7. return i;
  8. }

用了一个临时的p来存储src,有没有必要,是没有必要的,调用时,形参只是copy一份实参的值,所以,src只是copy了一份

原理src的值。

源码:

  1. size_t __cdecl strlen ( const char * str)
  2. {
  3. const char *eos = str;
  4.  
  5. while( *eos++ ) ;
  6.  
  7. return( eos - str - );
  8. }

用递归求:

  1. int strlen4(const char *str)
  2. {
  3. if(*str=='\0')
  4. return ;
  5. else
  6. return strlen4(++str)+1;
  7. }

有点非常值得注意,最开始我写的是strlen4(str++),程序崩溃了,为什么?

每次函数内部调用时,str都指向h,strlen4(str)执行,str根本没动,导致了无穷递归,实在是一个很大的错误。

strncpy:

原型:char * strncpy(char *dest, char *src,size_tnum);
功能:(c/c++)复制src中的内容(字符,数字、汉字....)到dest,复制多少由num的值决定,返回指向dest的指针。如果遇到null字符('\0'),且还没有到num个字符时,就用(num - n)(n是遇到null字符前已经有的非null字符个数)个null字符附加到destination。注意:并不是添加到destination的最后,而是紧跟着由source中复制而来的字符后面。下面举例说明[1]
char des[] = "Hello,i am!";
char source[] = "abc\0def";
strncpy(des,source,5);
此时,des区域是这样的:a,b,c,\0,\0,逗号,i,空格,a,m,!
在测试时不要用prinf("%s"),因为遇到\0就结束了, 
  1. for(int i=;i<sizeof(d)/sizeof(d[]);i++)
  2. printf("%c",d[i]);
注意:\0,\0并不是添加在!的后面。
头文件:#include "string.h"
说明:
如果n > dest串长度,dest栈空间溢出产生崩溃异常。
否则:
1)src串长度<=dest串长度,(这里的串长度包含串尾NULL字符)
如果n<src串长度,src的前n个字符复制到dest中。但是由于没有NULL字符,所以直接访问dest串会发生栈溢出的异常情况。
如果n = src串长度,与strcpy一致。
如果n >src串长度,src串存放于desk字串的[0,src串长度],dest串的(src串长度, dest串长度]处存放NULL。
2)src串长度>dest串长度
如果n =dest串长度,则dest串没有NULL字符,会导致输出会有乱码。如果不考虑src串复制完整性,可以将dest最后一字符置为NULL。
综上,一般情况下,使用strncpy时,建议将n置为dest串长度(除非你将多个src串都复制到dest数组,并且从dest尾部反向操作),复制完毕后,为保险起见,将dest串最后一字符置NULL,避免发生在第2)种情况下的输出乱码问题。当然喽,无论是strcpy还是strncpy,保证src串长度<dest串长度才是最重要的。
我的实现:
  1. char *strncpy2(char *dest,char *src,int num)
  2. {
  3. char *p=dest;
  4. int i=;
  5. while( *src && (*p++=*src++) && (i<num))
  6. i++;
  7. if(i<num)
  8. {
  9. for(int j=;j<=num-i;j++)
  10. {
  11. *p++='\0';
  12. }
  13. }
  14. return dest;
  15. }

写这几行代码时经常发生各种各样的错误,如在for(j=1;j<num-i;j++)内,j写成了i,导致与前面的i重复。

*p++='\0'写成*p='\0';导致最后一个num字符没有赋0.

源码:

  1. /***
  2. *strncpy.c - copy at most n characters of string
  3. *
  4. * Copyright (c) Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines strncpy() - copy at most n characters of string
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <string.h>
  13.  
  14. /***
  15. *char *strncpy(dest, source, count) - copy at most n characters
  16. *
  17. *Purpose:
  18. * Copies count characters from the source string to the
  19. * destination. If count is less than the length of source,
  20. * NO NULL CHARACTER is put onto the end of the copied string.
  21. * If count is greater than the length of sources, dest is padded
  22. * with null characters to length count.
  23. *
  24. *
  25. *Entry:
  26. * char *dest - pointer to destination
  27. * char *source - source string for copy
  28. * unsigned count - max number of characters to copy
  29. *
  30. *Exit:
  31. * returns dest
  32. *
  33. *Exceptions:
  34. *
  35. *******************************************************************************/
  36.  
  37. char * __cdecl strncpy (
  38. char * dest,
  39. const char * source,
  40. size_t count
  41. )
  42. {
  43. char *start = dest;
  44.  
  45. while (count && (*dest++ = *source++)) /* copy string */
  46. count--;
  47.  
  48. if (count) /* pad out with zeroes */
  49. while (--count)
  50. *dest++ = '\0';
  51.  
  52. return(start);
  53. }

为什么我写的代码比官方写的代码要长,因为我用了一个变量i 来计算,看有没有达到num,官方则自己用num--来判断。在while

循环里用count来代替i<num.这样不加少了一个变量i,而且很紧凑。在后面的填充null时,也没用用j来技术,还是count字减。

官方的源码写的漂亮。

strrev:

自己写的:

  1. char * strrev2(char *src)
  2. {
  3. char *p,*q;
  4. p=src;
  5. q=src+strlen(src)-;
  6. while(p<q)
  7. {
  8. char tmp=*p;*p=*q;*q=tmp; //注意是char tmp,我已经习惯写成int tmp
  9. p++;
  10. q--;
  11. }
  12. return src;
  13. }

官方源码:

  1. char * __cdecl _strrev (
  2. char * string
  3. )
  4. {
  5. char *start = string;
  6. char *left = string;
  7. char ch;
  8.  
  9. while (*string++) /* find end of string */
  10. ;
  11. string -= 2;
  12.  
  13. while (left < string)
  14. {
  15. ch = *left;
  16. *left++ = *string;
  17. *string-- = ch;
  18. }
  19.  
  20. return(start);
  21. }

更多参考:

http://www.cnblogs.com/JCSU/articles/1305401.html

http://www.cnblogs.com/winnxm/archive/2011/05/17/2049266.html

http://www.cppblog.com/dawnbreak/archive/2009/08/24/94262.html

 

字符串操作函数<string.h>相关函数strcpy,strcat,等源码。的更多相关文章

  1. Python中字符串操作函数string.split('str1')和string.join(ls)

    Python中的字符串操作函数split 和 join能够实现字符串和列表之间的简单转换, 使用 .split()可以将字符串中特定部分以多个字符的形式,存储成列表 def split(self, * ...

  2. 转:C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文

    转自:C语言字符串操作函数 - strcpy.strcmp.strcat.反转.回文 C++常用库函数atoi,itoa,strcpy,strcmp的实现 作者:jcsu C语言字符串操作函数 1. ...

  3. C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文

    原文:http://www.cnblogs.com/JCSU/articles/1305401.html C语言字符串操作函数 1. 字符串反转 - strRev2. 字符串复制 - strcpy3. ...

  4. C语言的常用字符串操作函数(一)

    一直做的是单片机相关的程序设计,所以程序设计上更偏向底层,对于字符串的操作也仅限于液晶屏幕上的显示等工作,想提高下字符串操作的水平,而不是笨拙的数组替换等方式,翻看帖子发现C语言的字符串操作函数竟然这 ...

  5. LoadRunner中常用的字符串操作函数

    LoadRunner中常用的字符串操作函数有:                strcpy(destination_string, source_string);               strc ...

  6. [转载]c++常用字符串操作函数

    原文地址:c++常用字符串操作函数作者:Valsun 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source ...

  7. C语言字符串操作函数总结

    转载来源:https://blog.csdn.net/qq_33757398/article/details/81212618 字符串相关操作头文件:string.h 1.strcpy函数 原型:st ...

  8. c语言字符串库函数#include<string.h>

    字符串函数<string.h> 在头文件<string.h>中定义了两组字符串函数.第一组函数的名字以str开头:第二组函数的名字以mem开头.只有函数memmove对重叠对象 ...

  9. python3.4学习笔记(十五) 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)

    python3.4学习笔记(十五) 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) python print 不换行(在后面加上,end=''),prin ...

随机推荐

  1. IOS学习之蓝牙4.0

    1建立中心角色 1 2 3 #import <CoreBluetooth/CoreBluetooth.h>  CBCentralManager *manager;  manager = [ ...

  2. css布局之块上下左右居中

    以下方案的通用代码: HTML code: <div class="box"> <div class="content"> <!- ...

  3. 小猪猪逆袭成博士之C++基础篇(一)数据精度、强制类型转换、变量命名规则

    小猪猪逆袭成博士之C++基础篇(一) 关键词: 数据精度.强制类型转换.变量命名规则 综述: 1.大多数编程语言通过两种方式来进一步补充其特征:一是赋予程序员自定义数据类型的权利(C++中的类):二是 ...

  4. HDU2084-数塔

    描述: 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? 代码: 简单的动态规划 ...

  5. kafka集群配置与测试

    刚接触一些Apache Kafka的内容,用了两天时间研究了一下,仅以此文做相关记录,以供学习交流.  概念: kafka依赖的项: 1. 硬件上,kafka利用线性存储来进行硬盘直接读写. 2. k ...

  6. Array.Add () and += in PowerShell

    $newArray = @() $newArray.Add("Hello") If I create a new array, and using the method Add() ...

  7. verilog中读取文件中的字符串_modelsim高级仿真

    今天给个程序大家玩玩.因为今天遇到一个问题,就是要向UART发送指令,指令非常多,都是字符串.一直copy 函数 UART ("COMM_1");  UART ("COM ...

  8. [转]android Handler使用

    转 http://blog.csdn.net/new_abc/article/details/8184634 不过这个我看不懂 不知道为什么i的值可以接着增长... package com.examp ...

  9. Windows10 上运行Ubuntu Bash

    Windows10 上运行Ubuntu Bash 2016年4月6日,Windows 10 Insider Preview 发布的版本 14316,添加了Ubuntu Bash,在Windows上提供 ...

  10. 安装VMware vSphere 的目的就是在一台物理服务器上安装很多很多的虚拟机

    版权声明:本文为博主原创文章,未经博主允许不得转载. 我们安装VMware vSphere 的目的就是在一台物理服务器上安装很多很多的虚拟机,我们可以通过VMware vSphere Client直接 ...