0

我正在尝试用 C 编译我的程序,这些是我得到的错误:(请注意,我是这种语言的初学者。)

Excer3.c:在函数“addstudent”中:

Excer3.c:50: error: `student' undeclared (first use in this function)
Excer3.c:50: error: (Each undeclared identifier is reported only once
Excer3.c:50: error: for each function it appears in.)
Excer3.c:50: error: parse error before "newS"
Excer3.c:50: error: `newS' undeclared (first use in this function)
Excer3.c:50: error: parse error before ')' token
Excer3.c:52: error: `studentName' undeclared (first use in this function)
Excer3.c: At top level:
Excer3.c:59: error: parse error before '*' token
Excer3.c: In function `readdb':
Excer3.c:70: error: `students' undeclared (first use in this function)
Excer3.c:70: warning: passing arg 2 of `addstudent' makes integer from pointer without                 a cast

我的代码看起来像这样。该函数无法识别它给出的参数。:

#include <stdio.h>

struct student {
int studentnumber;
char* Name;
struct student* next;
};

struct teacher {
int teachernumber;
char* Name;
struct teacher* next;
};

struct course {
struct teacher teachers[5];
struct student students[50];
int semesternumber;
struct course* next;
};

int readline(char s[],int lim)
{
  int c, i;
  for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
    s[i] = c;
  if (c == '\n') {
    s[i] = c;
    ++i;
  }
  s[i] = '\0';
  return i;
}

int addstudent (struct student* prev, int studentnumber, char* studentname)
{
if (!(prev==NULL))
  student newS = (student*)malloc(sizeof(student));
  newS->studentnumber = studentnumber;
  newS->firstname = strdup(studentName);
  newS->next = NULL;
  prev->next=newS;

  return 1;
}

int readdb(student* students)
{
char line[200];
int* studentnumber,coursenumber,teachernumber,semesternumber;
char studentname[100],teachername[100],coursename[100];

while(readline(line,200) > 0)
{
if(sscanf(line, "S %d %s", &studentnumber, studentname)==2)
{
 printf("Student. \n\tStudent number: %d, \n\tFirst name: %s\n", studentnumber,  
studentname);
 addstudent(students,&studentnumber,studentname);
 }
else if(sscanf(line, "C %d %s %d", &coursenumber, coursename , &semesternumber)==3)
   printf("Course. \n\tCourse number: %d \n\tCourse name: %s \n\tSemester: %d\n",   
coursenumber, coursename, semesternumber);
else if(sscanf(line, "E %d %d", &studentnumber, &coursenumber)==2)
printf("Enrolment. \n\tStudent number: %d, \n\tCourse number: %d\n", studentnumber, 
coursenumber);
else if(sscanf(line, "T %d %s", &teachernumber, teachername)==2)
 printf("Teacher. \n\tTeacher number: %d, \n\tFirst name: %s\n", teachernumber,  
teachername);
else if(sscanf(line, "A %d %s", &teachernumber, &coursenumber)==2)
 printf("Assignment. \n\tTeacher number: %d, \n\tCourse number: %d\n", teachernumber,
coursenumber);
}

}


int main () 
{
struct student* students = NULL;

readdb(&students);
return 0;

}
4

3 回答 3

3

你正在使用student而不是struct student如果你想使用,student那么你需要typedef struct student student;在你的代码中。

于 2012-09-08T17:24:28.870 回答
1

addstudent中,您声明

student newS = (student*)malloc(sizeof(student));

那应该是struct student newS

于 2012-09-08T17:23:42.977 回答
1

现有答案是正确的:student目前不是您程序中的类型名称,而是struct student。你已经正确地使用了它,所以让它保持一致。

冒着偏离代码审查领域的风险,addstudent仍然不会做你想做的事。

int addstudent (struct student* prev, int studentnumber, char* studentname)
{
    if (!(prev==NULL))
        student newS = (student*)malloc(sizeof(student));
    newS->studentnumber = studentnumber;
    newS->firstname = strdup(studentName);
    newS->next = NULL;
    prev->next=newS;

    return 1;
}

所以这是试图追加到链表的末尾,这很好,但是有一些错误:

  • newS声明应该是struct student *newS = malloc(sizeof(*newS));

    1. 我们修复了编译器抱怨的结构名称
    2. malloc 返回一个指针,所以newS也必须是一个指针
    3. p = malloc(sizeof(*p))是一种常见的习惯用法,可以避免p以后更改类型时出现bug,但忘记编辑sizeof表达式
  • 您当前newS 在 if 语句的范围内声明 - 这意味着以下引用的newS行将无法编译,因为它们看不到该变量。我们可以使用大括号来扩展范围,如下所示:if (prev) { ... } return 1;

  • newS->next始终设置为 NULL,因此如果已经有值,则将其丢弃prev->next这意味着您的链接列表只能是一个学生。如果你想让你的列表链接在一起,它应该是

    newS->next = prev->next;
    prev->next = newS;
    
  • 你正在混淆struct student **(这是传入的类型readdb(&students))并且struct student *......你需要弄清楚你想要哪个,并坚持下去。当前addstudent代码仅在您有一个struct student实例(不是指针)且 next 初始化为 NULL 作为列表的头部时才有效。

于 2012-09-08T17:42:44.263 回答