gpt4 book ai didi

c - 链表函数调用

转载 作者:行者123 更新时间:2023-11-30 15:52:35 25 4
gpt4 key购买 nike

我正在尝试准备我的项目,但是void insertCourse(CourseNodePtr* cr, char* code);函数给出参数 2 的语法错误; switch case 1 中的代码。有人知道它为什么不起作用吗?

     struct course{
char code[10];
char name[40];
char instructor[40];
char term[10];
int year;
struct course *coursePtr; /* pointer to next node */
};

typedef struct course Course; /* synonym for struct course */
typedef Course *CourseNodePtr; /* synonym for CourseNodePtr* */



/* Function Prototypes */
void insertCourse(CourseNodePtr* cr, char* code);
char* deleteCourse(CourseNodePtr* cr, CourseNodePtr* inscr, char* code);
void insertStudent(FILE* filePtr, StudentNodePtr* cr, int id);
int deleteStudent(FILE* filePtr, StudentNodePtr* cr, int id);
void displayStudent(FILE* filePtr, int id);
void displayClassNumbers(FILE* filePtr, int id);
void displayRecvCourse (FILE* filePtr, char* code, char* term, int year);
void registration(FILE* filePtr);
void instructions( void );

int main(void)
{
CourseNodePtr startPtr = NULL; /* initially there are no nodes */
StudentNodePtr startPtr1 = NULL; /* initially there are no nodes */
int choice; /* user's choice */

Course code;
Student id;
Course year;


instructions(); /* display the menu */
printf( "? " );
scanf( "%d", &choice );

while (choice != 9) {
switch (choice) {
case 1:

printf("Pls enter the course code to be inserted.\n");
scanf("%s", code);
insertCourse( &startPtr, char* code);
break;
/*


printf( "? " );
scanf( "%d", &choice );


printf( "End of run.\n" );

getch();
return 0; /* indicates successful termination */
} /* end main */



/* Insert course function */
void insertCourse(CourseNodePtr* cr, char* code)
{
CourseNodePtr newPtr; /* New node pointer */
CourseNodePtr previousPtr; /* previous node pointer in list */
CourseNodePtr currentPtr; /* current node pointer in list */

newPtr = malloc( sizeof(Course) ); /* memory allocation for new node */

if (newPtr != NULL) {
printf("Pls enter the code number of the course.\n");
scanf("%s", &(newPtr->code));
printf("Pls enter the course name.\n");
scanf("%s", &(newPtr->name));
printf("Pls enter the instructor name.\n");
scanf("%s", &(newPtr->instructor));
printf("Pls enter the term; Spring or Fall.\n");
scanf("%s", &(newPtr->term));
printf("Pls enter the year.\n");
scanf("%s", &(newPtr->year));
newPtr->coursePtr = NULL;

previousPtr = NULL;
currentPtr = *cr;

while ((currentPtr != NULL) && (code > currentPtr->code)) {
previousPtr = currentPtr;
currentPtr = currentPtr->coursePtr;
} /* End While */

if ( previousPtr == NULL ) {
newPtr->coursePtr = *cr;
*cr = newPtr;
} /* End if */
else {
previousPtr->coursePtr = newPtr;
newPtr->coursePtr = currentPtr;
} /* End else */
} /* End if */

else {
printf( " %c could not be inserted. Memory not enough...\n", code);
} /* End else */
} /* End function insert */

最佳答案

您不能输入这样的结构。 %s 读取的是字符串,而不是结构。您必须逐一阅读各个字段。或者将代码更改为字符串类型或动态分配的字符数组。

编辑:也在这里:

insertCourse( &startPtr, char* code);

您不能指定 char* 作为 code 的类型。相反,你应该进行强制转换:

insertCourse( &startPtr, (char*)code);

关于c - 链表函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14302266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com