字符串副本 [英] string copy
问题描述
char s1 =" this string";
char * s2;
s2 =(char *)malloc(30);
memset(s2,0,30);
/ *这假设将字符串s1复制到s2 * /
while((* s2 ++ = * s1 ++)!=''\''');
/ *为什么s2的空内容打印在这里* /
非常感谢!
char s1 = "this string";
char *s2;
s2 = (char *)malloc(30);
memset(s2, 0, 30);
/* this supposes to copy string s1 to s2 */
while((*s2++ = *s1++) != ''\0'');
/* Why empty content of s2 print out here*/
Thanks a lot!
推荐答案
bml写道:
char s1 =" this string" ;;
char * s2;
s2 =(char *)malloc(30);
memset(s2,0,30);
/ *这假设为复制字符串s1到s2 * /
while((* s2 ++ = * s1 ++)!=''\ 0'');
/ *为什么s2的空内容打印在这里* /
char s1 = "this string";
char *s2;
s2 = (char *)malloc(30);
memset(s2, 0, 30);
/* this supposes to copy string s1 to s2 */
while((*s2++ = *s1++) != ''\0'');
/* Why empty content of s2 print out here*/
提示:s2现在指向的是什么?
[BTW - 将来,发布*真实*代码(最小可能的
runnable代码段);否则我们只是在猜测。]
HTH,
- g
-
Artie Gold - 德克萨斯州奥斯汀
Hint: What is s2 now pointing to?
[BTW -- in the future, post *real* code (the most minimal possible
runnable snippet); otherwise we''re just guessing.]
HTH,
--ag
--
Artie Gold -- Austin, Texas
bml写道:
char s1 =" this string" ;;
^^缺少''*''
char * s2;
s2 =(char *)malloc(30);
^^^^^^^傻演员
无错误检查memset(s2,0,30);
^^^^^^^^^^^^^^^^^
无用的函数调用
/ *这假设将字符串s1复制到s2 * /
while((* s2 ++ = * s1 ++)!=''\ 0'');
/ *为什么s2的空内容打印在这里* /
你不走运;你的电脑应该爆炸了。
因为s2指向了字符串的末尾。你认为是什么?
当你增加它时会发生什么?
非常感谢!
char s1 = "this string"; ^^ missing ''*''
char *s2;
s2 = (char *)malloc(30); ^^^^^^^ silly cast
No error check memset(s2, 0, 30); ^^^^^^^^^^^^^^^^^
useless function call
/* this supposes to copy string s1 to s2 */
while((*s2++ = *s1++) != ''\0'');
/* Why empty content of s2 print out here*/ You were unlucky; your computer should have exploded.
Because s2 points beyond the end of the string. What did you think was
happening when you incremented it?
Thanks a lot!
#include< ; stdio.h>
#include< string.h>
#include< stdlib.h>
int main(无效)
{
char * s1 =" this string" ;;
char * t1 = s1;
char * s2;
char * t2;
if(!(s2 = malloc(strlen(s1)+ 1))){
fprintf(stderr,malloc为s1 \ n而失败);
退出(EXIT_FAILURE);
}
t2 = s2;
while((* t2 ++ = * t1 ++));
printf(" s1:\"%s \" \ ns2: \"%s \" \ n",s1,s2);
返回0;
}
-
Martin Ambuhl
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char *s1 = "this string";
char *t1 = s1;
char *s2;
char *t2;
if (!(s2 = malloc(strlen(s1) + 1))) {
fprintf(stderr, "malloc failed for s1\n");
exit(EXIT_FAILURE);
}
t2 = s2;
while ((*t2++ = *t1++)) ;
printf("s1: \"%s\"\ns2: \"%s\"\n", s1, s2);
return 0;
}
--
Martin Ambuhl
" bml" <乐***** @ yahoo.com>写道:
"bml" <le*****@yahoo.com> writes:
char s1 =" this string" ;;
我认为这是一个错字,你打算写一下
char * s1 =" this string";
char * s2;
s2 =(char *)malloc(30);
不要施放malloc。如果您忘记包含< stdlib.h>这不是必需的并且可能隐藏严重错误
(提供
malloc的原型)。
memset(s2,0,30);
确保在访问s2之前malloc成功。
if(s2!= NULL)
/ *使用s2这里* /
如果你只是想清空字符串然后* s2 =''\'''会这样做。
/ *这假设将字符串s1复制到s2 * /
而((* s2 ++ = * s1 ++)! =''\''');
为什么不简单地使用strcpy?
/ *为什么s2的空内容打印在这里* /
char s1 = "this string";
I assume this is a typo and you meant to write
char *s1 = "this string";
char *s2;
s2 = (char *)malloc(30);
Don''t cast malloc. It is not necessarry and may hide a serious error
if you forget to include <stdlib.h> (which provide the prototype for
malloc).
memset(s2, 0, 30);
Make sure malloc succeeded before accessing s2.
if (s2 != NULL)
/* use s2 here */
If you simply want to "empty" the string then *s2 = ''\0'' will do.
/* this supposes to copy string s1 to s2 */
while((*s2++ = *s1++) != ''\0'');
Why not simply use strcpy?
/* Why empty content of s2 print out here*/
循环完成后s2指向哪里?你需要记住
原始指针,
char * t = s2;
while(* s2 ++ = * s1 ++);
s2 = t;
printf("%s \ n",s2);
Where does s2 point after the loop completes? You''ll need to remember
the original pointer,
char *t = s2;
while (*s2++ = *s1++);
s2 = t;
printf ("%s\n", s2);
这篇关于字符串副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!