hellogo
新手上路

UID 83363
精华
0
积分 6
帖子 2
金钱 6 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2006-11-17
状态 离线
|
一个指针问题?望高手指教!
#include <stdio.h>
char *strcat(char *dst,char *src){
char *t;
t=dst;
while(*t!='\0'){
++t;
}
while(*src!='\0'){
*t=*src;
t++;
src++;
}
*t='\0';
return dst;
}
void main() {
char *str1,*str2;
char *p="hello",*q="world";
str1=p;
str2=q;
printf("%s\n", strcat(str1,str2));
}
由于作业的要求,自己用指针写了一个字符串连接的函数。
上面的程序测试成功,输出helloworld,
但当连接两个相同的字符串时就有问题了,我想输出hellohello
例如把上面的 printf("%s\n", strcat(str1,str2)); 改为
printf("%s\n", strcat(str1,str1)); 为什么输出不了hellohello
望高手指教。谢谢了!
|
|