C语言中的形实参双向传递该怎么做?

lbiori241 2006-02-02 11:20:51
C语言中规定的是实参-〉形参的单向传递方式,这种方式有些时候很不方便,我在一本书上看到说用指针的方法可以解决这个问题,大虾们能否说说怎么样实现双向传递呢?比如这个题目:
编写一个void InitStack(Stack *s)的栈的初始化函数,功能是建立由s指向的一个栈,并初始化栈顶,栈底指针和栈的长度。其中Stack是定义的一个描述栈的结构体,将这个结构体看作一个对象,s是指向这个对象的指针。因为函数的返回值为void型,所以必须使得在函数中所作的初始化能够传递到主函数中,即双向传递。
我试着写了一个,但还是没有实现双向传递,迷茫中,请大虾赐教
栈空间定义:
#define STACK_INIT_SIZE 10 //initial sizes
#define STACK_INCREMENT 2 //size-increment
结构体定义:
struct stack //define the stack struction
{
int * base; //base pointer,which point the base of the stack
int * top; //top pointer,whick point the top of the stack
int stackSize; //the size of the stack
};
...全文
1140 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
whacking 2006-02-03
  • 打赏
  • 举报
回复
用引用不就可以了,实参传进去之后要分配一个内存空间,再用引用去传你要传出动去的参数
引用可以在全局区声明
megaboy 2006-02-03
  • 打赏
  • 举报
回复
......
#define STACK_INIT_SIZE 10 //initial sizes
#define STACK_INCREMENT 2 //size-increment
......
struct stack //define the stack struction
{
int * base; //base pointer,which point the base of the stack
int * top; //top pointer,whick point the top of the stack
int stackSize; //the size of the stack
};
......
int main(void)
{
struct stack * s;
s=(struct stack*)malloc(sizeof(struct stack));
........
InitStack(s);
........
return 0;
}

void InitStack(Stack *s)
{
.....
s->base = .....
s->top = ......
return;
}
lbiori241 2006-02-03
  • 打赏
  • 举报
回复
以上是链栈初始化必须的函数,创建链表CreatList,和求链表长度ListLength应该没有问题,就是链栈初始化InitStack出现问题。
在InitStack中CreatList(STACK_INIT_SIZE)是构造链栈空间,大小为10,构造出链栈之后,s->base相当于是链的头指针,那么求这个指针指向的链表长度应该是10,这个结果在InitStack中的输出得到验证,可是为什么在主函数中同样的语句输出却是0呢?不解,拜求大虾解惑
lbiori241 2006-02-03
  • 打赏
  • 举报
回复
还是有问题,头痛了。把部分代码贴出来大家帮我找找看哪里出现问题吧。我构造的是链栈
#define STACK_INIT_SIZE 10
#define STACK_INCREMENT 2
#define NULL 0
#define SUCCESS 1
#define FAILURE 0
#define LEN sizeof(LNode)
typedef int DATA;
struct list{
int num; /*序号*/
DATA nodeData;
struct LNode *next;
};/*链表结点结构定义*/
typedef struct list LNode;
struct stack{
LNode * base; /*base pointer,which point the base of the stack*/
LNode * top; /*top pointer,whick point the base of the stack*/
int stackSize; /*the size of the stack*/
};/*栈结构定义*/
typedef struct stack SqStack;
/*函数表:*/
LNode * CreatList(int length); /*此函数我已经单独测试过,没有问题*/
int ListLength(LNode *head); /*此函数我已经单独测试过,没有问题*/
int InitStack(SqStack *s); /*此函数有问题,但我不知道原因,请大虾们指教*/

/*链表的建立函数,参数为链表的长度,返回指向头节点的指针*/
LNode * CreatList(int length)
{
int i;
LNode *head,*p1,*p2;
if(length<=0)
{return NULL;}
else
{
head = p1 = p2 = (LNode *)malloc(LEN);
if(head == NULL)
{return NULL;}
else
{
head->num = 1;
head->nodeData = 0;
head->next = NULL;
for(i=0;i<length-1;i++)
{
if((p1 = (LNode *)malloc(LEN)) == NULL)
{
p1 = head;
while(p1 != NULL) /*free all of the node that applied ago*/
{
head = head->next;
free(p1);
p1 = head;
}
return NULL;
}
else
{
p1->num = i+2; /*write the sequence number*/
p1->nodeData = 0; /*initialize the node'data*/
p2->next = p1;
p2 = p1;
}
}
p2->next = NULL;
return head;
}
}
}
/*返回已存在的链表的长度,参数为链表的头指针*/
int ListLength(LNode *head)
{
int length;
LNode *p1;
if(head == NULL)
{
return 0;
}
else
{
p1 = head;
length = 1;
while((p1->next != NULL)&&(p1->next != head))
{
p1 = p1->next;
length = length + 1;
}
return length;
}
}
/*链栈的初始化函数,参数为指向栈的指针,返回成功or失败*/
int InitStack(SqStack *s)
{
s->base = CreatList(STACK_INIT_SIZE);
printf("%d\n",ListLength(s->base)); /*此输出是为了测试用,在结果中此输出为10*/
if(s->base = NULL)
{return FAILURE;}
else
{
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
return SUCCESS;
}
}

main()
{
DATA elem;
LNode * p1;
SqStack *s;
s = (SqStack *)malloc(sizeof(struct stack));
InitStack(s);
printf("%d\n",ListLength(s->base));/*此输出为0,与InitStack函数中的输出不一致,问题出在哪呢?*/
getch();
}
lbiori241 2006-02-02
  • 打赏
  • 举报
回复
对,将Stack看成一个对象,在main函数中要先申明一个指向它的指针:Stack *s;
ZeroGts 2006-02-02
  • 打赏
  • 举报
回复
void InitStack(Stack *s)这样的话s就要预先分配空间。
lbiori241 2006-02-02
  • 打赏
  • 举报
回复
感谢楼上两位的关注,但是我还是不太明白,要求定义的函数void InitStack(Stack *s)中的参数是个一级指针,而lexchou(龍子龍孫)给出的定义中参数是二级指针,这样正确吗?
ZeroGts 2006-02-02
  • 打赏
  • 举报
回复
void InitStack(Stack **s)
lexchou 2006-02-02
  • 打赏
  • 举报
回复
void InitStack(Stack **s)
{
struct stack* pStack=(struct stack*)malloc(sizeof(struct stack));
...
...

**s=pStack;
}

70,017

社区成员

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

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