重庆市六十六中:数据结构问题

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/30 00:33:56
#include<alloc.h>
#define NULL 0
typedef struct list_node *list_pointer;
typedef struct list_node{
int data;
list_pointer next;
};
list_pointer ptr=NULL;
ptr=(list_pointer)malloc(sizeof(list_node));
main()
{
ptr->data=50;
ptr->next=NULL;
printf("%d",ptr->data);
}
我按书上打进去错误就在"list_pointer ptr=NULL;
ptr=(list_pointer)malloc(sizeof(list_node));"
请教高手指点

在函数体外只允许“声明”全局变量,而并非定义,所以“ptr=(list.......”这句中的ptr就是个未定义的变量,就会出现错误。

另外,你的第二个typedef也是不良好的,缺了第二个参数,编译器应该会报warning。

所以,我的修改如下:

#include<malloc.h>
#define NULL 0

typedef struct list_node *list_pointer;
struct list_node{
int data;
list_pointer next;
};
main()
{
list_pointer ptr=NULL;
ptr=(list_pointer)malloc(sizeof(list_node));
ptr->data=50;
ptr->next=NULL;
printf("%d",ptr->data);
}

#include <malloc.h>
#define NULL 0

typedef struct list_node *list_pointer;
typedef struct list_node{
int data;
list_pointer next;
}list_node;
main()
{
list_pointer ptr=NULL;
ptr=(list_pointer )malloc(sizeof(list_node));
ptr->data=50;
ptr->next=NULL;
printf("%d",ptr->data);
}


list_pointer ptr=NULL;ptr前少了个星号*