为什么在isstudent里面我已经指到东西了,在函数里面输出是正常的但是在返回在外面就不行了


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>

typedef struct book{
    char name[16];
    char id[8];        //存八位编号
    int loaned;        //借出
    int num;        //总数
    double price;    //书价格
    char publisher[32];    //出版社
    char author[32];        //作者
    struct book*next;
}book;

typedef struct student{
    char id[8];            //学号
    char name[16];        //姓名
    int overdue;        //逾期数
    char borrow_book_name[5][8];    //借的书的名字,最大为5,用编号存
    int borrow_book_num=0;            //借书数
    struct student*next;
}student;

book* Initialisation_book();
student*Initialisation_student();
student* Is_student_id(char *id);

int main(){
    book *Head=Initialisation_book();
    student*Student=Initialisation_student();
    char student_id[16]={0};
    printf("请输入你的学号:");
    scanf("%s",student_id);
    
    if(Is_student_id(student_id)!=NULL){
        student*p=Is_student_id(student_id);
        printf("%s\n",p->borrow_book_name[0]);
    }
    return 0;
}


student* Is_student_id(char *id){
    student sd,*p=NULL;
    FILE*fp=NULL;
    fp=fopen("student.dat","r");
    if(fp==NULL){
        printf("打开文件失败!");
        exit(1);
    }
    int flag=0;
    fseek(fp,0,SEEK_SET);
    while(!feof(fp)){
        fread(&sd,sizeof(student),1,fp);
        if(strcmp(sd.id,id)==0){
            p=&sd;
            flag=1;
            break;
        }
    }
    //printf("%s\n",p->borrow_book_name[0]);
    return p;
}
book* Initialisation_book(){
    book *tail=NULL,*Head=NULL,*sd;
    FILE*fp=NULL;
    long size;
    fp=fopen("library.dat","r"); 
    if(fp==NULL){
        printf("打开文件失败!");
        exit(1);
    }
    fseek(fp, 0, SEEK_END); // 将文件指针移动到文件末尾
    size = ftell(fp); //获取文件大小
    fseek(fp, 0, SEEK_SET); // 将文件指针移回文件开头
    while(ftell(fp) != size){ 
        sd=(book*)malloc(sizeof(book));
        sd->next=NULL;
        if(fread(sd,sizeof(book),1,fp) != 1){ 
            free(sd);
            break; 
        }
        if(Head){
            tail->next=sd;
            tail=tail->next;
        }else{
            Head=sd;
            tail=sd;
        }
    }
    fclose(fp); 
    tail=NULL;
    return Head;
}

student*Initialisation_student(){
    student *tail=NULL,*Head=NULL,*sd;
    FILE*fp=NULL;
    long size;
    fp=fopen("student.dat","r");
    if(fp==NULL){
        printf("文件打开失败!");
        return NULL;
    }
    fseek(fp,0,SEEK_END);
    size = ftell(fp);
    fseek(fp,0,SEEK_SET);
    while(ftell(fp)!=size){
        sd=(student*)malloc(sizeof(student));
        sd->next=NULL;
        if(fread(sd,sizeof(student),1,fp)!=1){
            free(sd);
            break;
        }
        if(Head){
            tail->next=sd;
            tail=tail->next;
        }else{
            Head=sd;
            tail=sd;
        }
    }
    fclose(fp);
    tail=NULL;
    return Head;
}

这么改,改动处见注释,供参考:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>

typedef struct book {
    char name[16];
    char id[8];        //存八位编号
    int loaned;        //借出
    int num;        //总数
    double price;    //书价格
    char publisher[32];    //出版社
    char author[32];        //作者
    struct book* next;
}book;

typedef struct student {
    char id[8];            //学号
    char name[16];        //姓名
    int overdue;        //逾期数
    char borrow_book_name[5][8];    //借的书的名字,最大为5,用编号存
    int borrow_book_num = 0;            //借书数
    struct student* next;
}student;

book* Initialisation_book();
student* Initialisation_student();
student* Is_student_id(char* id);

int main() {
    book* Head = Initialisation_book();
    student* Student = Initialisation_student();
    char student_id[16] = { 0 };
    printf("请输入你的学号:");
    scanf("%s", student_id);
    student* p = Is_student_id(student_id);          // 修改
    if (p) {  //if (Is_student_id(student_id) != NULL)  修改
             //student* p = Is_student_id(student_id);  修改
        printf("%s\n", p->borrow_book_name[0]);
    }
    return 0;
}


student* Is_student_id(char* id) {
    student *sd = (student*)malloc(sizeof(student)), * p = NULL; //修改
    FILE* fp = NULL;
    fp = fopen("student.dat", "r");
    if (fp == NULL) {
        printf("打开文件失败!");
        return  p;  //exit(1);   修改
    }
    int flag = 0;
    fseek(fp, 0, SEEK_SET);
    while (1) {   //while (!feof(fp)) 修改    
        if (fread(sd, sizeof(student), 1, fp) == 1) {   //修改
            if (strcmp(sd->id, id) == 0) {
                p = sd;
                flag = 1;
                break;
            }
        }
        else       //修改
            break; //修改
    }
    fclose(fp);    // 修改                
    //printf("%s\n",p->borrow_book_name[0]);
    return p;
}
book* Initialisation_book() {
    book* tail = NULL, * Head = NULL, * sd;
    FILE* fp = NULL;
    long size;
    fp = fopen("library.dat", "r");
    if (fp == NULL) {
        printf("打开文件失败!");
        exit(1);
    }
    fseek(fp, 0, SEEK_END); // 将文件指针移动到文件末尾
    size = ftell(fp); //获取文件大小
    fseek(fp, 0, SEEK_SET); // 将文件指针移回文件开头
    while (ftell(fp) != size) {
        sd = (book*)malloc(sizeof(book));
        sd->next = NULL;
        if (fread(sd, sizeof(book), 1, fp) != 1) {
            free(sd);
            break;
        }
        if (Head) {
            tail->next = sd;
            tail = tail->next;
        }
        else {
            Head = sd;
            tail = sd;
        }
    }
    fclose(fp);
    tail = NULL;
    return Head;
}

student* Initialisation_student() {
    student* tail = NULL, * Head = NULL, * sd;
    FILE* fp = NULL;
    long size;
    fp = fopen("student.dat", "r");
    if (fp == NULL) {
        printf("文件打开失败!");
        return NULL;
    }
    fseek(fp, 0, SEEK_END);
    size = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    while (ftell(fp) != size) {
        sd = (student*)malloc(sizeof(student));
        sd->next = NULL;
        if (fread(sd, sizeof(student), 1, fp) != 1) {
            free(sd);
            break;
        }
        if (Head) {
            tail->next = sd;
            tail = tail->next;
        }
        else {
            Head = sd;
            tail = sd;
        }
    }
    fclose(fp);
    tail = NULL;
    return Head;
}

是指 Is_student_id 这个方法吗?
这个里面的 student sd 是个局部变量,函数返回后,局部变量将销毁,指针 p 指向的内存地址可能被释放,也可能被其它程序使用。将它声明为静态变量即可:static student sd;,或者使用动态内存分配函数(如malloc)分配内存。