BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / soft-design / #25328同步于 2008/5/12
该镜像源已超过 30 天没有更新,可能在源站已被删除。
SoftDesign机器人发帖

这个小程序哪里错了?

ChrisPaul
2008/5/12镜像同步8 回复
//连接两个字符串 #include<stdio.h> #include<stdlib.h> int main() { char *lian(char *x,char *y); char a[100],b[100],c[100]; puts("please input 2 strings:a and b"); gets(a); gets(b); c[100]=*lian(a,b); puts(c); system("pause"); } char *lian(char *x,char *y) {int i,j; char m[100]; for(i=0;;i++) {if(x[i]=='\0') break; m[i]=x[i];} for(j=0;y[j]!='\0';j++,i++) m[i]=y[j]; m[i]=y[j]; return m; }
订阅后,新回复会通过你的通知中心匿名送达。
8 条回复
rebirthatsix机器人#1 · 2008/5/12
囧,你返回一个局部变量的地址出去做甚?
flyingmiao机器人#2 · 2008/5/12
【 在 ChrisPaul 的大作中提到: 】 : //连接两个字符串 : #include<stdio.h> : #include<stdlib.h> : ................... m是子程序里的临时变量数组,空间在堆栈上分配,子程序返回后,m的生命周期也就结束,m消失了。所以你在主程序里再访问m的时候,自然会有错误
chingphon机器人#3 · 2008/5/14
看了你的代码后,谈谈我的感觉: 1.char *lian(char *x,char *y) 其中“lian”不要用汉语拼音,选个能表达函数功能的简单英文单词,动词或动词短语; 函数lian()返回的是指针(地址),但是c[100]=*lian(a,b);应该不能完成期望的功能吧; 2.int main() 有返回值是吧?因为我看你写的是int,而不是void,但我在main函数中没有找到return之类的语句; 3.关于作用域和生命周期 被调用函数中的“局部变量”在该函数完成使命后,自动销毁,死。上哪儿找去?所以切忌返回“局部变量”! 【 在 ChrisPaul 的大作中提到: 】 : //连接两个字符串 : #include<stdio.h> : #include<stdlib.h> : ...................
wks机器人#4 · 2008/5/14
#include<string.h> strcat王道!!!!! 或者最简单的 void mystrcat(char* left, char* right) { while(*left++=*right++); } 稍微优雅一点 void my_beautiful_strcat(char* left, char* right) { while(*right != '\0') { *left = *right; left++; right++; } *left='\0'; }
rebirthatsix机器人#5 · 2008/5/15
【 在 wks 的大作中提到: 】 : #include<string.h> : strcat王道!!!!! : 或者最简单的 : ................... 显然lz不知道或者没打算用strcat这个函数。。。
he1l0机器人#6 · 2008/5/16
为什么我觉得这是strcpy呢 【 在 wks 的大作中提到: 】 : #include<string.h> : strcat王道!!!!! : 或者最简单的 : ...................
pmps机器人#7 · 2008/5/17
【 在 he1l0 的大作中提到: 】 : 为什么我觉得这是strcpy呢 同觉得
coolfantasy机器人#8 · 2008/5/17
gets()这个函数是强烈不推荐用的