It says it's a violation of access, but I don't know if you declared it or if it's out of memory...I don't know the cause.

Asked 2 years ago, Updated 2 years ago, 88 views

It says Access violation but I don't know the cause.

What I want is a program that takes the class, name, and department and makes it look pretty We tried to float in two ways: return the structure and return the pointer of the structure It literally stopped saying it was an access violation. ㅠ<

It's okay when you build, but you get into trouble when you debug it. Help me

Is it a memory issue?

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

/////////// function first, and the body is below.

structure student///////////////structure declaration
{
    int id;
    char name[20];
    char major[20];
};

structure student readStudent1()//////////////// function that reads the structure and enters it into the structure
{
    struct student stud;

    printf ("%s %7s %6s \n", "school year", "name", "school"));
    if (scanf_s("%d %s %s", &stud.id, &stud.name, &stud.major) == 3)
    {
        return stud;
    }
    else
    {
        printf ("input error\n");
    }
}

structure student *readStudent2()//////////////// function that reads the structure and enters it as a structure pointer
{
    struct student *s;

    s = (struct student *) malloc(sizeof(struct student));

    printf("%s%7s%6s\n", "Class", "Name", "Department:");

    if (scanf_s("%d %s %s", &s->id, s->name, s->major) == 3)
    {
        return s;
    }
    else
    {
        return NULL;
    }
}

void printStudent1(struct student)////////// Output to structure
{
    printf("+------------------------------------------------------+\n");
    printf("| Grade: %7s Name: %6s Major: %8s | \n", stud.id, stud.name, stud.major);
    printf("+------------------------------------------------------+\n");
}

void printStudent2(struct student*s)///////////// output to structure pointer
{
    printf("+------------------------------------------------------+\n");
    printf("| Grade: %7s Name: %6s Major: %8s | \n", s->id, s->name, s-> major);
    printf("+------------------------------------------------------+\n");
}

int main()//////////////// body
{
    struct student stud, *p;

    stud = readStudent1();
    printStudent1(stud);
    p = readStudent2();
    printStudent2(p);
    return 0;

}

struct pointer

2022-09-22 12:35

1 Answers

Class number: %7s is d, not s.


2022-09-22 12:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.