首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

指针 求帮忙,该如何解决

2012-04-22 
指针求帮忙#define N 10#includestdio.h#includestdlib.hint main(){int ichar *s1,*s2s1(char *)m

指针 求帮忙
#define N 10
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i;
char *s1,*s2;
s1=(char *)malloc(10);
s2=(char *)malloc(10);
printf("please input string1!\n");
scanf("%s",s1);
printf("After input String1!\n");
printf("%s\n",s1);

while(*s1!='\0')
{
*s2=*s1;
s2++;
s1++;
}
*s2='\0';
printf("After copy String2:");
printf("%s\n",s2);
return 0;
}


我还没有输出s2,它的内容就被释放了,什么原因??

[解决办法]
#define N 10
#include<stdio.h>
#include<stdlib.h>
int main()
{
 int i;
 char *s1,*s2,*s2_beg;
 s1=(char *)malloc(10);
 s2=(char *)malloc(10);
 printf("please input string1!\n");
 scanf("%s",s1);
 printf("After input String1!\n");
 printf("%s\n",s1);
 
s2_beg=s2;
while(*s1!='\0')
 {
 *s2=*s1;
 s2++;
 s1++;
 }
 *s2='\0';
 s2=s2_beg;
 printf("After copy String2:");
 printf("%s\n",s2);
 return 0;
}

[解决办法]
主要是你s2的值改变了..指向改变了.

热点排行