输入a-f任何一个字母,打印为abcdefg

不爱吃酸的大力水手 2013-03-28 10:27:25
char *expend(char s1[], char s2[]);
int main()
{
//char ch;
char s1[100];
char s2[100];
printf("input s1 = ");
scanf("%c", &s1);
printf("%s\n", expend(s1, s2));
system("pause");
return 0;
}
char *expend(char s1[], char s2[])
{
int i;
char s3[] = {"abcdefg"};
for (i = 0; s1[i] != '\0'; ++i)
switch(s1[i]) {
case 'a':case 'b':case 'c':case 'd':
case 'e':case 'f':case 'g':
printf("%s\n", s3);
break;
}
return s3;
}
1.为什么生成过程会这样
2.为什么结果会这样
两个问题,求解释?
...全文
221 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
AnYidan 2013-03-29
  • 打赏
  • 举报
回复
引用 2 楼 wxj198904 的回复:
有问题啊,char *expend(char s1[], char s2[]) { int i; char s3[] = {"abcdefg"}; for (i = 0; s1[i] != '\0'; ++i) switch(s1[i]) { case 'a':case 'b':case 'c':case 'd': case 'e':case 'f':case 'g'……
s3是个临时变量啊,你不能返回临时变量的地址啊
  • 打赏
  • 举报
回复
你们觉得我哪里比较模糊,应该学习哪个方面啊??
shen_wei 2013-03-29
  • 打赏
  • 举报
回复
全局和局部的区别。。
wawLwxj 2013-03-28
  • 打赏
  • 举报
回复
把 char s3[] = {"abcdefg"};改为 static char s3[] = {"abcdefg"};
wawLwxj 2013-03-28
  • 打赏
  • 举报
回复
有问题啊,char *expend(char s1[], char s2[]) { int i; char s3[] = {"abcdefg"}; for (i = 0; s1[i] != '\0'; ++i) switch(s1[i]) { case 'a':case 'b':case 'c':case 'd': case 'e':case 'f':case 'g': printf("%s\n", s3); break; } return s3; } s3是个临时变量啊,你不能返回临时变量的地址啊
wawLwxj 2013-03-28
  • 打赏
  • 举报
回复
没有对数组初始化啊,用memset(si, 0, 100); memset(s2, 0, 100);都初始化为0,再试试

69,379

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧