【发布时间】:2014-06-20 13:15:07
【问题描述】:
我想使用指针将一个字符串复制到另一个字符串,并且我从 printf 函数获取垃圾值(一些未知字符)。它有什么问题? 输出是“复制字符串**”。在**得到一些未知字符的地方。
#include <stdio.h>
#include <conio.h>
void main()
{
char *s="string for copy",*c,*temp;//temp is used to back the
//pointer on 1st position
clrscr();
while(*s!=NULL)
{
*c=*s
c++;
s++;
}
c='\0';
c=temp;//back pointer to first position
printf("String from c:);
puts(c);
getch();
}
【问题讨论】:
-
main的返回类型始终为int!*s!=NULL是一个不好的比较:NULL 指针常量指向char。完全放弃比较,这是多余的。正确的缩进对于快速阅读很重要。 -
欢迎来到 SO。请阅读How to Ask 和help center 了解如何提问,尤其是关于MCVE 的部分。我还建议阅读 How to Debug Small Programs 以更好地了解如何调试它。
标签: c