大神急救,急救啊,真的急救啊,程序怎么错,真的看不出。
#include<stdio.h>
#include<stdlib.h>
#define m 3
#define n 4
#define p 5
typedef struct Student
{
float score;
struct Student *next;
}Student;
Student *CreateAndInit(int Nodes,float Lscore[]);
/*
** main function
*/
int main(void)
{
float LAscore[m] = {1,3,4};
float LBscore[n] = {3,4,5,6};
float LCscore[p] = {1,3,4,6,7};
Student *LA = NULL, *LB = NULL, *LC = NULL;
//Create and initialize the link
LA = CreateAndInit(m,LAscore);
LB = CreateAndInit(n,LBscore);
LC = CreateAndInit(p,LCscore);
return 0;
}
//Create and initialize the link
Student *CreateAndInit(int Nodes,float Lscore[])
{
int i;
Student *head = NULL, *p = NULL, *p1 = NULL;
head = p = p1 = (Student *)malloc(sizeof(Student));
head -> next = NULL;
head ->score = Lscore[0];
for(i = 0; i < Nodes - 1; ++i)
{
p = (Student *)malloc(sizeof(Student));
p -> next = NULL;
p -> score = Lscore[i + 1];
p1 ->next = p;
p1 = p;
}
return head;
}
错误提示:H:\安装软件\VC6.0\MSDev98\Bin\test.cpp(37) : error C2059: syntax error : 'constant'
我觉得好像程序没有错。请问这个程序是怎么了。
[解决办法]
Student *CreateAndInit(int Nodes,float Lscore[])
{
Student *h;
Student *p1;
Student*p2;
h = p1 = p2 = new Student();
h->next = NULL; h->score = Lscore[0];
for(int i = 0; i < Nodes - 1; ++i)
{
p2 = new Student();
p2->next = NULL;
p2->score = Lscore[i + 1];
p1->next = p2;
p1 = p2;
}
return h;
}
不知道为什么,把p改成p2就没事了!还有一个问题,你这个程序会造成内存泄露啊
[解决办法]
#define?p?5
Student?*head?=?NULL,?*p?=?NULL,?*p1?=?NULL;
编译预处理时,宏替换出错。
[解决办法]
对,对,难怪呢!上面还有一个p呢
[解决办法]
你37行的那个变量p被宏替代成了5
相当于
Student *head = ((void *)0), *5 = ((void *)0), *p1 = ((void *)0);
这教育你,宏命名应该奇葩一点,别用太普通的变量名,省的到时候哪里错了都不知道
[解决办法]
这是问题,换个名吧。
#define p 5