Why fread functions in C language can't use structure pointers

I use the fread () function to write to my own defined structure pointer, but this reports an error. The
problem occurs where I commented.

typedef struct Student {
    char *name;
} Student;

int main() {
    Student stu = {12, "Danny"};
    FILE *out;
    if ((out = fopen("out.dat", "w")) == NULL) {
        printf("ERROR\n");
        return 0;
    }

    fwrite(&stu, sizeof(Student), 1, out);
    fclose(out);


    FILE *in;
    if((in = fopen("out.dat", "r")) == NULL) {
        printf("ERROR\n");
        return 0;
    }

    Student *stu2;
    fread(stu2, sizeof(Student), 1, in); // 
    printf("%s", stu2->name);
    return 0;
}
C
Jul.04,2022

how to write text content without a single wild pointer allocated memory

Menu