当前位置: 代码迷 >> C语言 >> 不用形参指针,而用函数内部变量能否实现同样的功能
  详细解决方案

不用形参指针,而用函数内部变量能否实现同样的功能

热度:245   发布时间:2006-09-24 11:46:48.0
以下是引用unicorn在2006-9-24 11:44:10的发言:

如果变量只是想在递归函数体内使用就可以实现,例

程序代码:

void recursion(int a,int b)
{
a--;
b++;
if(n==0) return;
recursion(a,b);
printf(\"递归函数:%d\n\",b);// 虽然b是局部变量但每次调用,b的值都变化
}

为什么不用static呢?
----------------解决方案--------------------------------------------------------
不过用了static不方便赋值是倒是一个麻烦
----------------解决方案--------------------------------------------------------

为什么不用static呢?

这个树遍历是递归过程,函数体中的变量对于每次循环来讲应该相当于static变量,不用在加static限制
----------------解决方案--------------------------------------------------------

比如这样...
void recursion()
{
static int a = 1;
static int b = 5;
int n = (a++) - (b--);
if(n==0) return;
printf("递归函数:a=%d,b=%d\n",a,b);// 虽然b是局部变量但每次调用,b的值都变化
recursion();
}


int main(void)
{
recursion();
}


----------------解决方案--------------------------------------------------------

可是楼主不想要你这样呀,楼主不是说想把那个m去掉么?


----------------解决方案--------------------------------------------------------
嗯 没看清楚
传进来的是m节点个数 对节点个数进行操作 最后节点个数还得有当前值...
看来不用指针 局部变量是实现不了了
----------------解决方案--------------------------------------------------------
他的题目我都没看明白

value(p->leftchile);
value(p->rightchile);

??这样用也太...
----------------解决方案--------------------------------------------------------

终于领悟到楼主的意思了
楼主的愿望是可以用static实现的


----------------解决方案--------------------------------------------------------

#include "stdio.h"

typedef struct node
{
char data;
struct node* leftchile;
struct node* rightchile;
}Bitree;//定义二叉树结点类型


value(Bitree* p,int k)//k为要求的位置,m是指针,
{
static int m = 0; //你要m,我就给你m
if(m++==k)
{
printf("K的值:%d",p->data);
return;
}
value(p->leftchile,k);
value(p->rightchile,k);
}


----------------解决方案--------------------------------------------------------
版主就是版主,厉害,我怎么没有想到呢,真心的感谢版主了,万分感谢,谢谢!
----------------解决方案--------------------------------------------------------
  相关解决方案