帮忙解决C语言

panweinick 2003-09-29 09:25:15
#include "stdlib.h"
#include "stdio.h"
struct node
{int a;
struct node *next;
};

void xx(top)
struct node *top;
{top=(struct node *)malloc(sizeof(struct node));
top->a=4;
top->next=NULL;
return;
}

main()
{struct node *top=NULL;
xx(top);
printf("%d\n",top->a);
}
为什么输出为0
我想输出为4 怎么编?在线等
...全文
35 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
junmayang 2003-10-03
  • 打赏
  • 举报
回复
#include "stdlib.h"
#include "stdio.h"
struct node
{int a;
struct node *next;
};

void xx(*top)
struct node **top;
{(*top)=(struct node *)malloc(sizeof(struct node));
(*top)->a=4;
(*top)->next=NULL;
return;
}

main()
{struct node *top=NULL;
xx(&top);
printf("%d\n",top->a);
}

改成这样
panweinick 2003-09-29
  • 打赏
  • 举报
回复
谢谢了!!!!!
simclock 2003-09-29
  • 打赏
  • 举报
回复
#include <stdio.h>

struct node
{int a;
struct node *next;
};

void xx(struct node **top)
{ *top=(struct node *)malloc(sizeof(struct node));
(*top)->a=4;
(*top)->next=NULL;
return;
}

main()
{

struct node *top;
xx(&top);
printf("%d\n",top->a);
}

改动最小的情况
panweinick 2003-09-29
  • 打赏
  • 举报
回复
#include "stdio.h"
struct node
{int a;
struct node *next;
};

void xx(top)
struct node *top;
{ top=(struct node *)malloc(sizeof(struct node));
top->a=4;
top->next=NULL;
return;
}

main()
{

struct node top;
xx(&top);
printf("%d\n",top->a);
}
被改成程序如上
我想建立一个链式存储空间
simclock 2003-09-29
  • 打赏
  • 举报
回复
现在你的程序是个什么样子?贴出来看看
panweinick 2003-09-29
  • 打赏
  • 举报
回复
我试过了 这里还有问题
printf("%d\n",top->a);
子函数内建立的空间怎么使用?
great_chenliang 2003-09-29
  • 打赏
  • 举报
回复
xx(&top);
lesheng 2003-09-29
  • 打赏
  • 举报
回复
注意变量的作用域
函数xx(top)中参数top是作为局部变量使用,作如下修改如何
void xx(top)
struct node *top;
{ // top=(struct node *)malloc(sizeof(struct node));
top->a=4;
top->next=NULL;
return;
}

main()
{
// 注意定义一个非指针变量
struct node top;
xx(&top);
printf("%d\n",top->a);
}
试试如何?
panweinick 2003-09-29
  • 打赏
  • 举报
回复
可以了
多谢了!!!!!!!
playboyxp 2003-09-29
  • 打赏
  • 举报
回复
#include "stdlib.h"
#include "stdio.h"
struct node
{int a;
struct node *next;
};

struct node *xx(top)//返回一个指针
struct node *top;
{top=(struct node *)malloc(sizeof(struct node));
top->a=4;
top->next=NULL;
return top;
}

main()
{struct node *top=NULL;
top=xx(top);//把值赋给top
printf("%d\n",top->a);
}
waYeah 2003-09-29
  • 打赏
  • 举报
回复
把{//top=;改为{ /*top= */
panweinick 2003-09-29
  • 打赏
  • 举报
回复
还是输出为0
hcj2002 2003-09-29
  • 打赏
  • 举报
回复
void xx(top)
struct node *top;
{top=(struct node *)malloc(sizeof(struct node));
top->a=4;
top->next=NULL;
return;
}
有错误,可该为:
void xx(struct node* top)
{
top=(struct node *)malloc(sizeof(struct node));
top->a=4;
top->next=NULL;
return;
}
panweinick 2003-09-29
  • 打赏
  • 举报
回复
{//top=;
这个部分出错
panweinick 2003-09-29
  • 打赏
  • 举报
回复
我在机上试过了
句型错误!!
「已注销」 2003-09-29
  • 打赏
  • 举报
回复
这样就行了!

#include "stdlib.h"
#include "stdio.h"
struct node
{int a;
struct node *next;
};

void xx(struct node *top)

{//top=;
top->a=4;
top->next=NULL;
return;
}

void main()
{
struct node *top=(struct node *)malloc(sizeof(struct node));
xx(top);
printf("%d\n",top->a);
}
zxm521b 2003-09-29
  • 打赏
  • 举报
回复
将void xx(top) 改为struct node *xx(void)
将return 改为 return top
将xx(top);改为top=xx();

69,369

社区成员

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

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