【发布时间】:2014-04-25 13:10:06
【问题描述】:
我是 c 编程的新手,我的代码正在运行,但我的问题是,如果我在 main 函数中声明 struct node *a,*b;,如何将 a 和 b 传递给 void create()。为什么它不起作用,有人可以帮我理解吗?
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int d;
struct node *next;
}*start=NULL;struct node *a,*b; //move this part to main function -> struct node *a,*b; but its not working
void create()
{
a=(struct node *)malloc(sizeof(struct node));
printf("Enter the data : ");
scanf("%d",&a->d);
a->next=NULL;
if(start==NULL)
{
start=a;
b=a;
}
else
{
b->next=a;
b=a;
}
}
void display()
{
struct node *a;
printf("\nThe Linked List : ");
a=start;
while(a!=NULL)
{
printf("%d--->",a->d);
a=a->next;
}
printf("NULL\n");
}
void main()
{
char ch;
do
{
create();
printf("Do you want to create another : ");
ch=getche();
}
while(ch!='n');
display();
}
void freenodes()
{
struct node *a;
a = start;
while(a != NULL)
{
struct node *freenode = a ;
a = a->next;
free(freenode) ;
}
}
【问题讨论】:
-
请定义您所说的“不工作”是什么意思。我们无法读懂你的想法。
-
什么“不工作”?
-
也许读过一些 C 教程?
标签: c parameter-passing