这个为什么编译通不过
typedef struct{
int num;
int math;
int english;
int computer;
}student;
typedef struct {
student stud;
struct linklist *next;
}linklist,*linknode;
(linknode)creat_node(linknode L)
{L=(linknode)malloc(sizeof(linklist));
L-> next=NULL;
return l;
}
---------编译结果:
错误 temp.c 12: 'linknode '的宣告
错误 temp.c 12: 类型不匹配在 'linknode '的宣告中
错误 temp.c 12: 说明语法错误
警告 temp.c 17: 未定义的构造 'linklist '
tc for windows 4.0 xp
(tc 2.0下也是这样)
[解决办法]
改为如下在VC6.0下编译通过:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int num;
int math;
int english;
int computer;
} student;
typedef struct
{
student stud;
struct linklist *next;
} linklist, *linknode;
linknode
creat_node(void)
{
linknode L;
L = (linknode)malloc(sizeof(linklist));
L-> next = NULL;
return L;
}
void main(void)
{
linknode L;
L=creat_node();
if( NULL==L )
{
printf( "Warming, the memory point to NULL!\n " );
return;
}
else
printf( "L=%d\n ", (int)L );
}
对于上面的错误,上面的改写位置是对的:
(linknode*)creat_node(linknode* L)
{L=(linknod*e)malloc(sizeof(linklist));
既然已经是参数了,就不必要再去给它分配内存了.