VS2013中strcat的问题?

weilizhi12 2014-12-04 01:40:17
#include<stdio.h>
#include<string.h>
void main()
{
char str1[20] = {"china"};
char str2[] = {"great"};
printf("%s",strcat_s(str1,20,str2));
}

运行不出结果,编译时会闪退
...全文
297 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-12-04
  • 打赏
  • 举报
回复
因为: errno_t strcat_s( char *strDestination, size_t numberOfElements, const char *strSource ); 而不是你想象的 char *strcat_s( char *strDestination, size_t numberOfElements, const char *strSource ); ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_vccrt/html/0f2f9901-c5c5-480b-98bc-f8f690792fc0.htm
 Collapse AllExpand All      Code: All Code: Multiple Code: Visual Basic Code: C# Code: Visual C++ Code: J# Code: JScript  
Visual Basic
C#
Visual C++
J#
JScript
Run-Time Library Reference 
strcat_s, wcscat_s, _mbscat_s 
Example  See Also  Send Feedback 
 

Append a string. These are versions of strcat, wcscat, _mbscat with security enhancements as described in Security Enhancements in the CRT.

 
errno_t strcat_s(
   char *strDestination,
   size_t numberOfElements,
   const char *strSource 
);
errno_t wcscat_s(
   wchar_t *strDestination,
   size_t numberOfElements,
   const wchar_t *strSource 
);
errno_t _mbscat_s(
   unsigned char *strDestination,
   size_t numberOfElements,
   const unsigned char *strSource 
);
template <size_t size>
errno_t strcat_s(
   char (&strDestination)[size],
   const char *strSource 
); // C++ only
template <size_t size>
errno_t wcscat_s(
   wchar_t (&strDestination)[size],
   const wchar_t *strSource 
); // C++ only
template <size_t size>
errno_t _mbscat_s(
   unsigned char (&strDestination)[size],
   const unsigned char *strSource 
); // C++ only
 

Parameters
strDestination
Null-terminated destination string buffer.

numberOfElements
Size of the destination string buffer.

strSource
Null-terminated source string buffer.

Return Value
Zero if successful; an error code on failure.

strDestination
 numberOfElements
 strSource
 Return value
 Contents of strDestination
 
NULL or unterminated
 any
 any
 EINVAL
 not modified
 
any
 any
 NULL
 EINVAL
 strDestination[0] set to 0
 
any
 0, or too small
 any
 ERANGE
 strDestination[0] set to 0
 

Remarks
The strcat_s function appends strSource to strDestination and terminates the resulting string with a null character. The initial character of strSource overwrites the terminating null character of strDestination. The behavior of strcat_s is undefined if the source and destination strings overlap.

Note that the second parameter is the total size of the buffer, not the remaining size:

  Copy Code 
char buf[16];
strcpy_s(buf, 16, "Start");
strcat_s(buf, 16, " End");               // Correct
strcat_s(buf, 16 – strlen(buf), " End"); // Incorrect
 

wcscat_s and _mbscat_s are wide-character and multibyte-character versions of strcat_s. The arguments and return value of wcscat_s are wide-character strings; those of _mbscat_s are multibyte-character strings. These three functions behave identically otherwise.

If strDestination is a null pointer, or is not null-terminated, or if strSource is a NULL pointer, or if the destination string is too small, the invalid parameter handler is invoked, as described in Parameter Validation . If execution is allowed to continue, these functions return EINVAL and set errno to EINVAL.

In C++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically (eliminating the need to specify a size argument) and they can automatically replace older, non-secure functions with their newer, secure counterparts. For more information, see Secure Template Overloads.

The debug versions of these functions first fill the buffer with 0xFD. To disable this behavior, use _CrtSetDebugFillThreshold.

TCHAR.H routine 
 _UNICODE & _MBCS not defined
 _MBCS defined
 _UNICODE defined
 
_tcscat_s
 strcat_s
 _mbscat_s
 wcscat_s
 

Requirements
Routine
 Required header
 
strcat_s
 <string.h>
 
wcscat_s
 <string.h> or <wchar.h>
 
_mbscat_s
 <mbstring.h>
 

For additional compatibility information, see Compatibility in the Introduction.

Example
See the code example in strcpy_s, wcscpy_s, _mbscpy_s.

.NET Framework Equivalent
System::String::Concat

See Also
Concepts
String Manipulation (CRT)
strncat, _strncat_l, wcsncat, wcsncat_l, _mbsncat _mbsncat_l
strncmp, wcsncmp, _mbsncmp, _mbsncmp_l
strncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l
_strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l
strrchr, wcsrchr, _mbsrchr, _mbsrchr_l
strspn, wcsspn, _mbsspn, _mbsspn_l
Send feedback on this topic to Microsoft.
图灵狗 2014-12-04
  • 打赏
  • 举报
回复
改为: char str1[20] = "china"; char str2[] = "great"; 试试看。

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧