vc++编译问题

[复制链接]
查看11 | 回复1 | 2008-8-11 23:39:52 | 显示全部楼层 |阅读模式
下面是一个将两个字符串进行相连的程序,请帮忙看一下错误出在哪.
#include
void fun(char p1[], char p2[])
{
char *p=p1,*q=p2;
while(p!=NULL)
p++;
*p='-';
p=p+1;
while(q!=NULL)
*p++=*q++;
*p='\0';
}
voidmain()
{

char s1[80], s2[40] ;

printf("Enter s1 and s2:\n") ;

scanf("%s%s", s1, s2) ;

printf("s1=%s\n", s1) ;

printf("s2=%s\n", s2) ;

printf("Invoke fun(s1,s2):\n") ;

fun(s1, s2) ;

printf("After invoking:\n") ;

printf("%s\n", s1) ;
}

回复

使用道具 举报

千问 | 2008-8-11 23:39:52 | 显示全部楼层
这代码写得很糟糕……首先没注意到缓冲区问题s1 s2数组在栈里开的,要注意输入的字符串不能太长了程序错在while(p!=NULL)while(q!=NULL) 这两处,本来是想判断字符串的结束符\0的,现在变成判断指针了正确写法应该是while(*p!='\0')while(*q!='\0')
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行