A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 军团指挥官 中级黑马   /  2015-3-24 10:37  /  1093 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. #include "stdio.h"
  2. void up_and_down(int);

  3. int main(void)
  4. {
  5.         up_and_down(1);
  6.         return 0;
  7. }

  8. void up_and_down(int n)
  9. {
  10.         printf("Level %d:n location %p\n",n,&n);
  11.         if(n<4)
  12.                 up_and_down(n+1);
  13.         printf("Level %d:n location %p \n",n,&n);
  14. }
复制代码



这是在《C primer plus》中看的一个例子,输出结果为:
Level 1:n location 0xbfcac790
Level 2:n location 0xbfcac770
Level 3:n location 0xbfcac750
Level 4:n location 0xbfcac730
Level 4:n location 0xbfcac730
Level 3:n location 0xbfcac750
Level 2:n location 0xbfcac770
Level 1:n location 0xbfcac790


前面的level1—level4都可以明白,后面的就看不明白怎么回事了,想请看明白的朋友解答一下。感激不尽!

6 个回复

倒序浏览
本帖最后由 谢囧 于 2015-3-24 10:52 编辑

up_and_down(n+1);
等于
printf("Level %d:n location %p\n",n+1,&n);
if(n+1<4)
{
        up_and_down(n+1+1);
}
printf("Level %d:n location %p \n",n+1,&n);
以此类推
回复 使用道具 举报
递归调用,逐次退出子程序
回复 使用道具 举报
看明白了。
回复 使用道具 举报
不难理解啊
回复 使用道具 举报

当时没看明白。你是准备第几期?
回复 使用道具 举报
军团指挥官 发表于 2015-4-3 09:15
当时没看明白。你是准备第几期?

8期吧 你呢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马