strcpy和strcat问题

编程入门 行业动态 更新时间:2024-10-27 17:17:48
本文介绍了strcpy和strcat问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将两个字符串组合在一起..并输入另一个字符串。怎么 我可以做。我尝试自己..使用以下代码。但似乎无法得到我想要的 结果..我想得到的结果是c是abcd 。 #include< stdio.h> #include< string.h> void main(){ char a [2] =" ab"; char b [2] =" cd"; char c [4] =" \0" ;; strcpy(c,a); strcat(c,b); printf(" a is% s \ n",a); printf(" b is%s \ nn,b); printf(" c is%s \\ \\ n,c); } i只能得到这个结果 a是?b ?? b是??? b ?? c是ab ???? b ?? 我的编码有什么问题?有什么不对吗? 请帮忙!!。 谢谢 JC ps。如果我使用strcpy(c,ab);和strcat(c," cd");我可以得到结果..c是 abcd"

解决方案

" JC" < JC@JC>在留言新闻中写道:bl ******** @ rain.i-cable ...

我想把两个字符串组合在一起..并且放入另一个字符串。我该怎么办?我尝试自己..使用以下代码。但似乎无法得到我想要的结果..我想得到的结果是c是abcd 。 #include< stdio.h> #include< string.h> void main(){ int main() { char a [2] =" ab" ;; char a [] =" ab"; char b [2] =" cd" ;; char b [] =" cd"; char c [4] =" \0英寸; char c [sizeof a + sizeof b + 1] = {0}; strcpy(c,a); strcat(c ,b); printf(a is%s \ n,a); printf(b is%s \ n,b); printf( c是%s \ n,c); } 我只得到这个结果 a是吗?b ?? b是??? b ?? c是ab ???? b ?? 我的编码有什么问题? 您的阵列没有为字符串终止符提供空间。 有什么不对吗? 是的。未定义的行为。 -Mike 请帮助!! 谢谢 JC ps。如果我使用strcpy(c,ab); 好​​到目前为止。文字包含一个终结符。 和strcat(c,cd);我可以得到结果..c是 不行,来自文字cd的终结符将 写入c [4] - 超出范围 - 未定义 行为。 abcd"

只是偶然。 -Mike

" JC" < JC@JC>写道:

我想把两个字符串组合在一起..并放入另一个字符串。我该怎么办?我尝试自己..使用以下代码。但似乎无法得到我想要的结果..我想得到的结果是c是abcd 。 #include< stdio.h> #include< string.h> void main(){未定义的行为,使用: int main(无效) { char a [2] =" ab"; char b [2] =" cd"; char c [4] =" \0英寸; 未定义的行为,你为a,b和c预留了更少的内存。 请记住:" xy"是一个由_three_字符组成的字符串文字, ''x'',''y''和隐式终止''\ 0''。在c的初始值设定项中明确使用''\0'' 是不必要的。 char a [3] =" ab" ;; char b [3] =" cd"; char c [5] =" " ;; strcpy(c,a); strcat(c,b); printf(" a is%s \ n", a); printf(b is%s \ nn,b); printf(c is%s\ n,c); } 我只得到这个结果 一个是?b ?? b是??? b ?? c是ab ???? b? ? 我的编码有什么问题?有什么不对吗?请帮助!! 见上文。 ps。如果我使用strcpy(c,ab);和strcat(c," cd");我可以得到结果..c是 abcd

这只是偶然的,见上文。 问候 Irrwahn - 错误103:硬盘中死老鼠。

2003年9月27日星期六00:37:31 + 0800,JC < JC@JC>写道:

我想将两个字符串组合在一起..并放入另一个字符串。我该怎么办?我尝试自己..使用以下代码。但似乎无法得到我想要的结果..我想得到的结果是c是abcd 。 #include< stdio.h> #include< string.h> void main(){ int main(void ) main返回一个int。 char a [2] =" ab" ;; 字符串ab实际上有三个字符('''',''b''和终止NUL 字符''\ 0''),但你只在数组中分配了两个[ ]。 将此更改为 char a [3] =" ab"; 或 char a [] =" ab"; 在后一种情况下,编译器将为 你。 char b [2] =" cd" ;; char c [4] =" \0英寸; 同上。 请注意,c []可能只包含三个字符加上终止NUL,所以 你会想要增加分配空间以准备你的字符串 连接如下: char c [5] =''\'0' ; / * 4个字符+终止NUL * / strcpy(c,a); strcat(c,b); printf(a is%s \ n ;,a); printf(b is%s \ nn,b); printf(c is%s \ n,c); 返回0;} ps。如果我使用strcpy(c,ab);和strcat(c," cd");我可以得到结果..c是 abcd

缓冲区溢出c。 仅仅因为某事是UB,并不意味着UB *的结果有* 与一个人的期望相反。 :-) - Robert B. Clark(电子邮件ROT13''ed) 访问ClarkWehyr Enterprises On-Line在 www.3clarks/ClarkWehyr/

hi, i want to combine two string together.. and put in to another string. how can i do . i try myself.. with the follow code. but seem can''t get the result i want.. i want to get the result with "c is abcd" . #include <stdio.h> #include <string.h> void main() { char a[2]="ab"; char b[2]="cd"; char c[4]=" \0"; strcpy(c,a); strcat(c,b); printf("a is %s\n",a); printf("b is %s\n",b); printf("c is %s\n",c); } i only get this result a is b? b is b? c is ab?b? what problem to my coding? anything wrong? please help!!. Thanks JC ps. if i use strcpy(c,"ab"); and strcat(c,"cd"); i can get the result.."c is abcd"

解决方案

"JC" <JC@JC> wrote in message news:bl********@rain.i-cable...

hi, i want to combine two string together.. and put in to another string. how can i do . i try myself.. with the follow code. but seem can''t get the result i want.. i want to get the result with "c is abcd" . #include <stdio.h> #include <string.h> void main() { int main() { char a[2]="ab"; char a[] = "ab"; char b[2]="cd"; char b[] = "cd"; char c[4]=" \0"; char c[sizeof a + sizeof b + 1] = {0}; strcpy(c,a); strcat(c,b); printf("a is %s\n",a); printf("b is %s\n",b); printf("c is %s\n",c); } i only get this result a is b? b is b? c is ab?b? what problem to my coding? Your arrays did not provide room for the string terminators. anything wrong? Yes. Undefined behavior. -Mike please help!!. Thanks JC ps. if i use strcpy(c,"ab"); OK so far. The literal contains a terminator. and strcat(c,"cd"); i can get the result.."c is Not OK, the terminator from the literal "cd" will be written to c[4] -- out of bounds -- undefined behavior. abcd"

Only by accident. -Mike

"JC" <JC@JC> wrote:

hi,i want to combine two string together.. and put in to another string. howcan i do . i try myself.. with the follow code. but seem can''t get theresult i want.. i want to get the result with "c is abcd" . #include <stdio.h>#include <string.h> void main() { undefined behaviour, use: int main( void ) { char a[2]="ab";char b[2]="cd";char c[4]=" \0"; Undefined behaviour, you reserved to less memory for a, b and c. Remember: "xy" is a string literal consisting of _three_ characters, ''x'', ''y'' and the implicit terminating ''\0''. The explicit use of ''\0'' in the initializer for c is unnecessary. char a[3]="ab"; char b[3]="cd"; char c[5]=" "; strcpy(c,a);strcat(c,b);printf("a is %s\n",a);printf("b is %s\n",b);printf("c is %s\n",c);}i only get this resulta is b?b is b?c is ab?b?what problem to my coding? anything wrong?please help!!. See above. ps. if i use strcpy(c,"ab"); and strcat(c,"cd"); i can get the result.."c isabcd"

This worked only by chance, see above. Regards Irrwahn -- ERROR 103: Dead mouse in hard drive.

On Sat, 27 Sep 2003 00:37:31 +0800, "JC" <JC@JC> wrote:

i want to combine two string together.. and put in to another string. howcan i do . i try myself.. with the follow code. but seem can''t get theresult i want.. i want to get the result with "c is abcd" .#include <stdio.h>#include <string.h>void main() { int main(void) main returns an int. char a[2]="ab"; The string "ab" actually has three chars (''a'', ''b'' and the terminating NUL character ''\0''), but you''ve only allocated two in array a[]. Change this to char a[3]="ab"; or char a[] = "ab"; In the latter case, the compiler will allocate sufficient storage (3) for you. char b[2]="cd";char c[4]=" \0"; Ditto. Note that c[] may hold only three characters plus the terminating NUL, so you''ll want to increase the allocated space in preparation for your string concatenation below: char c[5] = ''\0''; /* 4 chars + terminating NUL */ strcpy(c,a);strcat(c,b);printf("a is %s\n",a);printf("b is %s\n",b);printf("c is %s\n",c); return 0;} ps. if i use strcpy(c,"ab"); and strcat(c,"cd"); i can get the result.."c isabcd"

Buffer overrun in c. Just because something is UB, doesn''t mean that the result of that UB *has* to be counter to one''s expectations. :-) -- Robert B. Clark (email ROT13''ed) Visit ClarkWehyr Enterprises On-Line at www.3clarks/ClarkWehyr/

更多推荐

strcpy和strcat问题

本文发布于:2023-07-29 06:40:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1238184.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:strcpy   strcat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!