关于strcpy和strcat的问题

HotWish 2014-05-08 11:25:59
char* p1="abcd", *p2="ABCD",str[50]="xyz";
strcpy(str+2,strcat(p1+2, p2+1));
这段代码在编译时会报错, 不太明白, 谁能帮忙解释一下?
...全文
289 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
乐百川 2014-05-10
  • 打赏
  • 举报
回复
strcpy 和strcat(s1,s2)都得保证s1的大小足够存放操作后的结果,否则就会越界,你那样+1 +2更可能越界…… 你那两个指针指向的是常量字符串,貌似也有问题,应该是可读写的字符数组才行。最后,数组越界的结果谁也不知道,程序可能正确执行,可能显示略有问题,也可能整个程序直接崩溃……
a280769112 2014-05-10
  • 打赏
  • 举报
回复
vs2005版本以上要使用strcat_s,其他的像spintf也要改为sprintf_s 参数多了大小,防止缓冲区溢出,大概是这样
漂浮一生 2014-05-09
  • 打赏
  • 举报
回复
引用 9 楼 u013823973 的回复:
p1,p2是在常量区,使用数组可解决strcat问题;char *str[50] 你这是定义了指针数组
p1,p2在栈上,由系统分配和管理,p1,p2指向的内容是常量字符串
漂浮一生 2014-05-09
  • 打赏
  • 举报
回复
引用 5 楼 buyong 的回复:
char* p1="abcd" p1在常量区。改为 char p1[100]; strcpy(p1, "abcd");
说的不太准确吧,p1在栈上,是系统分配和管理的,p1指向的内容是常量字符串,在常量区
难题 2014-05-09
  • 打赏
  • 举报
回复
p1,p2是在常量区,使用数组可解决strcat问题;char *str[50] 你这是定义了指针数组
zybjtu 2014-05-09
  • 打赏
  • 举报
回复
strcat(p1+2, p2+1) 这句错了 char* p1 = "asdf" 字符串asdf在常量区, 所以不能更改 char p1[N]: 用strncpy给p1赋值 snprintf也可 这样就可以了
__cc__ 2014-05-09
  • 打赏
  • 举报
回复
#5正确
xiao0915 2014-05-09
  • 打赏
  • 举报
回复
const char* p1="abcd"
buyong 2014-05-09
  • 打赏
  • 举报
回复
char* p1="abcd" p1在常量区。改为 char p1[100]; strcpy(p1, "abcd");
xiaohuh421 2014-05-09
  • 打赏
  • 举报
回复
strcat(p1+2, p2+1) 这句话会导致内存访问越界. 错误那就是没跑的了. strcat本身是不安全的, 所以你要strcat时, 要自己保证目标缓冲区有足够大的内存空间.
「已注销」 2014-05-09
  • 打赏
  • 举报
回复
strcat的dest得有足够的空间
赵4老师 2014-05-09
  • 打赏
  • 举报
回复
strcat, wcscat, _mbscat Append a string. char *strcat( char *strDestination, const char *strSource ); wchar_t *wcscat( wchar_t *strDestination, const wchar_t *strSource ); unsigned char *_mbscat( unsigned char *strDestination, const unsigned char *strSource ); Routine Required Header Compatibility strcat <string.h> ANSI, Win 95, Win NT wcscat <string.h> or <wchar.h> ANSI, Win 95, Win NT _mbscat <mbstring.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns the destination string (strDestination). No return value is reserved to indicate an error. Parameters strDestination Null-terminated destination string strSource Null-terminated source string Remarks The strcat 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. No overflow checking is performed when strings are copied or appended. The behavior of strcat is undefined if the source and destination strings overlap. wcscat and _mbscat are wide-character and multibyte-character versions of strcat. The arguments and return value of wcscat are wide-character strings; those of _mbscat are multibyte-character strings. These three functions behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tcscat strcat _mbscat wcscat Example /* STRCPY.C: This program uses strcpy * and strcat to build a phrase. */ #include <string.h> #include <stdio.h> void main( void ) { char string[80]; strcpy( string, "Hello world from " ); strcat( string, "strcpy " ); strcat( string, "and " ); strcat( string, "strcat!" ); printf( "String = %s\n", string ); } Output String = Hello world from strcpy and strcat! String Manipulation Routines See Also strncat, strncmp, strncpy, _strnicmp, strrchr, strspn
赵4老师 2014-05-09
  • 打赏
  • 举报
回复
strcpy, wcscpy, _mbscpy Copy a string. char *strcpy( char *strDestination, const char *strSource ); wchar_t *wcscpy( wchar_t *strDestination, const wchar_t *strSource ); unsigned char *_mbscpy( unsigned char *strDestination, const unsigned char *strSource ); Routine Required Header Compatibility strcpy <string.h> ANSI, Win 95, Win NT wcscpy <string.h> or <wchar.h> ANSI, Win 95, Win NT _mbscpy <mbstring.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns the destination string. No return value is reserved to indicate an error. Parameters strDestination Destination string strSource Null-terminated source string Remarks The strcpy function copies strSource, including the terminating null character, to the location specified by strDestination. No overflow checking is performed when strings are copied or appended. The behavior of strcpy is undefined if the source and destination strings overlap. wcscpy and _mbscpy are wide-character and multibyte-character versions of strcpy. The arguments and return value of wcscpy are wide-character strings; those of _mbscpy are multibyte-character strings. These three functions behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tcscpy strcpy _mbscpy wcscpy Example /* STRCPY.C: This program uses strcpy * and strcat to build a phrase. */ #include <string.h> #include <stdio.h> void main( void ) { char string[80]; strcpy( string, "Hello world from " ); strcat( string, "strcpy " ); strcat( string, "and " ); strcat( string, "strcat!" ); printf( "String = %s\n", string ); } Output String = Hello world from strcpy and strcat! String Manipulation Routines See Also strcat, strcmp, strncat, strncmp, strncpy, _strnicmp, strrchr, strspn
yulinlang 2014-05-08
  • 打赏
  • 举报
回复
2.13 以下的初始化有什么区别?char a[] = "string literal"; char *p = "string literal"; 当我向 p[i] 赋值的时候, 我的 程序崩溃了。 字符串常量有两种稍有区别的用法。用作数组初始值 (如同在 char a[] 的声明中), 它指明该数组中字符的初始值。其它情况下, 它会转化为一个 无名的静态字符数组, 可能会存储在只读内存中, 这就是造成它不一定能被修改。 在表达式环境中, 数组通常被立即转化为一个指针 (参见第 6 章), 因此第二个声明把 p 初始化成 指向无名数组的第一个元素。 为了编译旧代码, 有的编译器有一个控制字符串是否可写的开关。 参见问题 1.11、 6.1、 6.2 和 6.6。 参考资料: [K&R2, Sec. 5.5 p. 104]; [ISO, Sec. 6.1.4, Sec. 6.5.7]; [Rationale, Sec. 3.1.4]; [H&S, Sec. 2.7.4 pp. 31-2]。 8.2 我的 strcat() 不行.我试了 char *s1 = "Hello, "; char *s2 = "world!"; char *s3 = strcat(s1, s2); 但是我得到了奇怪的结果。 跟前面的问题 7.1 一样, 这里主要的问题是没有正确地为连接 结果分配空间。C 没有提供自动管理的字符串类型。C 编译器只为源码 中明确提到的对象分配空间 (对于字符串, 这包括字符数组和串常量)。 程序员必须为字符串连接这样的运行期操作的结果分配足够的空间, 通常可以通过声明数组或调用 malloc() 完成。 strcat() 不进行任何分配; 第二个串原样不动地附加在第一个之后。 因此, 一种解决办法是把第一个串声明为数组: char s1[20] = "Hello, "; 由于 strcat() 返回第一个参数的值, 本例中为 s1, s3 实际上是多 余的; 在 strcat() 调用之后, s1 包含结果。 提问中的 strcat() 调用实际上有两个问题: s1 指向的字符串常数, 除了空间不足以放入连接的字符串之外, 甚至都不一定可写。 参见问题 1.13。 参考资料: [CT&P, Sec. 3.2 p. 32]。
merlinfang 2014-05-08
  • 打赏
  • 举报
回复
报什么错,反正我的VC没报错
max_min_ 2014-05-08
  • 打赏
  • 举报
回复
还是内存管理的问题,可更改和不可更改

65,184

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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