LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 461|回复: 2

奇怪的问题,郁闷四了

[复制链接]
发表于 2004-12-16 12:41:55 | 显示全部楼层 |阅读模式
我写了个函数,很简单的,用了递归
...

  1. static void digui(int number)
  2. {
  3.         static int i=0;
  4.         static int numstr[MAXLEN+1];
  5.         numstr[i++]=(number % 10);
  6.         printf("<%d>:after:%d,%d\n",i,numstr[i],number % 10);
  7.         if( number/10 == 0 )
  8.         {
  9.             .......
  10.             return;
  11.         }
  12.         digui(number/10);

  13. }
复制代码

....
发现执行了numstr[i++]=(number % 10);后,numstr还是都为0的数组
(从后面的printf可以看到)
但在gdb调试中确可以看到numstr数组的变化,


  1. ....
  2. Breakpoint 1, main () at testdigui.c:9
  3. 9               digui(num);
  4. (gdb) step
  5. digui (number=123) at testdigui.c:17
  6. 17              numstr[i++]=(number % 10);
  7. (gdb) p numstr
  8. $1 = {0 <repeats 11 times>}
  9. (gdb) step
  10. 18              printf("<%d>:after:%d,%d\n",i,numstr[i],number % 10);
  11. (gdb) p numstr
  12. $2 = {3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  13. (gdb) step
  14. <1>:after:0,3
  15. 19              if( number/10 == 0 )
  16. (gdb) step
  17. 23              digui(number/10);
  18. (gdb) step
  19. digui (number=12) at testdigui.c:17
  20. 17              numstr[i++]=(number % 10);
  21. (gdb) step
  22. 18              printf("<%d>:after:%d,%d\n",i,numstr[i],number % 10);
  23. (gdb) p numstr
  24. $3 = {3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  25. (gdb)
  26. ...
复制代码


程序执行(递归调用的参数123)
....
<1>:after:0,3
<2>:after:0,2
<3>:after:0,1
...
大虾们出来看看啊,谢谢


  1. numstr[i++]=(number % 10);
  2.         printf("<%d>:after:%d,%d\n",i,numstr[i],number % 10);
复制代码

自己看错了,i++ 了   晕
 楼主| 发表于 2004-12-16 12:47:29 | 显示全部楼层
自己不能删自己的帖子吗
发表于 2004-12-16 16:01:14 | 显示全部楼层
为了避免引起不必要的纠纷,只有版主能删除贴子。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表