如何在C中调用单链表中的字符串
How to call string in singly linked list in C
这是我的结构
struct Student
{
int numberofstudents;
float mid,prj,final,hmw;
int lettergrade;
int studentnumber;
char name[40];
char lastname[40];
int birthyear;
float totalgrade;
struct Student *next;
}* head;
我有这样的功能
void searchbylastname(char *lastname)
{
struct Student * temp = head;
while(temp!=NULL)
{
if(temp->lastname == lastname){
printf("Student Number: %d\n", temp->studentnumber);
printf("Name: %s\n", temp->name);
printf("Surname: %s\n", temp->lastname);
printf("Birth Year: %d\n", temp->birthyear);
printf("Total Grade: %0.2f\n", temp->totalgrade);
return;
}
temp = temp->next;
}
printf("Student with student number %s is not found !!!\n", lastname);
}
我在 main 中用 switch case 调用它
head = NULL;
int choice;
char name[50];
char lastname[50];
int birthyear;
int studentnumber;
float totalgrade;
float prj,hmw,mid,final;
case 6:
printf("Enter student lastname to search: ");
scanf("%s", &lastname);
searchbylastname(lastname);
break;
但它不能按姓氏搜索,它会自动将我定向到这里;
printf("Student with student number %s is not found !!!\n", lastname);
我不知道该怎么办,如果有人有意见,我将不胜感激。
==
不比较 C 中的字符串,只比较这些字符串的引用(地址)。如果他们不引用同一个对象,他们总是不同的。
要比较字符串,您需要使用 strcmp
函数。
https://www.man7.org/linux/man-pages/man3/strncmp.3.html
您的代码中有更多问题。即使您使用 strcmp
也会显示未找到该学生的消息。如果你可能有更多的学生有相同的 surmane(这更期望你需要添加一个标志)
void searchbylastname(char *lastname)
{
struct Student * temp = head;
int studentsFound = 0;
while(temp!=NULL)
{
if(!strcmp(temp->lastname,lastname)){
stutentsFound = 1;
printf("Student Number: %d\n", temp->studentnumber);
printf("Name: %s\n", temp->name);
printf("Surname: %s\n", temp->lastname);
printf("Birth Year: %d\n", temp->birthyear);
printf("Total Grade: %0.2f\n", temp->totalgrade);
}
temp = temp->next;
}
if(!studentsFound)
printf("Students having %s surname have not been found !!!\n", lastname);
}
这是我的结构
struct Student
{
int numberofstudents;
float mid,prj,final,hmw;
int lettergrade;
int studentnumber;
char name[40];
char lastname[40];
int birthyear;
float totalgrade;
struct Student *next;
}* head;
我有这样的功能
void searchbylastname(char *lastname)
{
struct Student * temp = head;
while(temp!=NULL)
{
if(temp->lastname == lastname){
printf("Student Number: %d\n", temp->studentnumber);
printf("Name: %s\n", temp->name);
printf("Surname: %s\n", temp->lastname);
printf("Birth Year: %d\n", temp->birthyear);
printf("Total Grade: %0.2f\n", temp->totalgrade);
return;
}
temp = temp->next;
}
printf("Student with student number %s is not found !!!\n", lastname);
}
我在 main 中用 switch case 调用它
head = NULL;
int choice;
char name[50];
char lastname[50];
int birthyear;
int studentnumber;
float totalgrade;
float prj,hmw,mid,final;
case 6:
printf("Enter student lastname to search: ");
scanf("%s", &lastname);
searchbylastname(lastname);
break;
但它不能按姓氏搜索,它会自动将我定向到这里;
printf("Student with student number %s is not found !!!\n", lastname);
我不知道该怎么办,如果有人有意见,我将不胜感激。
==
不比较 C 中的字符串,只比较这些字符串的引用(地址)。如果他们不引用同一个对象,他们总是不同的。
要比较字符串,您需要使用 strcmp
函数。
https://www.man7.org/linux/man-pages/man3/strncmp.3.html
您的代码中有更多问题。即使您使用 strcmp
也会显示未找到该学生的消息。如果你可能有更多的学生有相同的 surmane(这更期望你需要添加一个标志)
void searchbylastname(char *lastname)
{
struct Student * temp = head;
int studentsFound = 0;
while(temp!=NULL)
{
if(!strcmp(temp->lastname,lastname)){
stutentsFound = 1;
printf("Student Number: %d\n", temp->studentnumber);
printf("Name: %s\n", temp->name);
printf("Surname: %s\n", temp->lastname);
printf("Birth Year: %d\n", temp->birthyear);
printf("Total Grade: %0.2f\n", temp->totalgrade);
}
temp = temp->next;
}
if(!studentsFound)
printf("Students having %s surname have not been found !!!\n", lastname);
}