c语言初学求助
p1 = p2 = (struct student *)malloc(LEN);我这个跟malloc的示范用法一样,为啥会出missing ')' before ';'的错误。求助十分感谢!! 是不是少了分号了 贴全代码! qiuyouzhi 发表于 2020-2-12 17:13
贴全代码!
struct student *creat()
{
struct student *head;
struct student *p1 , *p2;
p1 = p2 = (struct student *)malloc(LEN);
printf("Please enter the num :\n");
scanf("%d",&p1->num);
printf("Please enter the score :\n");
scanf("%d",&p1->score);
head = NULL;
n = 0;
while(p1->num != 0)
{
n++;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
printf("\nPlease enter the num :\n");
scanf("%d",&p1->num);
printf("Please enter the score :\n");
scanf("%d",&p1->score);
}
p2->next = NULL;
return (head);
} 超级甲鱼粉 发表于 2020-2-12 17:31
struct student *creat()
{
struct student *head;
前面define 了一个 LEN 是 sizeof(struct student) 最后的魁拔 发表于 2020-2-12 17:05
是不是少了分号了
struct student *creat()
{
struct student *head;
struct student *p1 , *p2;
p1 = p2 = (struct student *)malloc(LEN);
printf("Please enter the num :\n");
scanf("%d",&p1->num);
printf("Please enter the score :\n");
scanf("%d",&p1->score);
head = NULL;
n = 0;
while(p1->num != 0)
{
n++;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
printf("\nPlease enter the num :\n");
scanf("%d",&p1->num);
printf("Please enter the score :\n");
scanf("%d",&p1->score);
}
p2->next = NULL;
return (head);
} 最后的魁拔 发表于 2020-2-12 17:05
是不是少了分号了
没有啊
应该是你之前的代码写错了 zzh_zzh_ 发表于 2020-2-12 17:34
应该是你之前的代码写错了
前面是特别简单代码感觉没错啊
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct student);
int n;
struct student
{
int num;
int score;
struct student *next;
};
struct student *creat();
void print(struct student *head);
void main()
{
struct student *stu;
stu = creat();
print(stu);
printf("\n\n");
}
struct student *creat()
{
struct student *head;
struct student *p1 , *p2;
p1 = p2 = (struct student *)malloc(LEN);
printf("Please enter the num :\n");
scanf("%d",&p1->num);
printf("Please enter the score :\n");
scanf("%d",&p1->score);
head = NULL;
n = 0;
while(p1->num != 0)
{
n++;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
printf("\nPlease enter the num :\n");
scanf("%d",&p1->num);
printf("Please enter the score :\n");
scanf("%d",&p1->score);
}
p2->next = NULL;
return (head);
}
void print(struct student *head)
{
struct student *p;
printf("There are %d records!\n\n",n);
p = head;
if(head)
{
do
{
printf("NO.%d student's score is %d\n",n,p->score);
p = p->next;
}while(p);
}
} 我找到错误了,我将你的代码试用了一下,发现也报错。然后我就看到了那个define。#define LEN sizeof(struct student);
此处多了一个分号。把分号去掉就没问题了。 {:10_249:}
页:
[1]