2

I'm a beginner in c I've been working on a program in C where using structure which represents students, and variable of structure contains the student's marks. My goal is to input marks for 2 students for 3 subjects each.

#include <stdio.h>

// Defining a structure for student details
struct Student {
    int mark;
};

int main() {
    struct Student st[2];

    // Input details for 2 student marks
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; j++) {
            printf("Marks: ");
            scanf("%f", &st[j].mark);
        }
    }

    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; j++) {
            printf("Marks: ", st[j].marks);
        }
    }

    return 0;
}

But had a confusion while writing it, As we're asking for 3 subjects for 2 students meaning students marks should run for 6 times in total. Won't it overwrite the Data ? Like

Marks: 5 Marks: 7 Marks: 8

Here, won't it overwrite the last data to the variable since we've not declared the array to store for 6 subjects,int mark[Num_of_subj]. In this case that should be 8 despite user entering 5 as the 1st Marks ?.

7
  • 1
    You can always run it to see if it indeed overwrites the structure member marks Commented Mar 14, 2024 at 6:30
  • Maybe what you intend to do is to have a list of grades in student ? Commented Mar 14, 2024 at 6:30
  • 1
    By the way here, st is an array of length 2 but you are accessing is with st[2] at some point, which will cause a buffer overflow. If I understood correctly, the line you want is scanf("%f", &st[i].mark[j]);, and define mark to be an array Commented Mar 14, 2024 at 6:34
  • Please include a copy of your console session when running your code. Commented Mar 14, 2024 at 6:37
  • 1
    But your code should not compile, as a Student struct does not have the marks member you are shown accessing in the second set of loops. Commented Mar 14, 2024 at 6:38

1 Answer 1

1

To answer your primary question, the code you provided will put the first grade entered in the first student mark, the second grade entered in the second student mark and the last grade entered will cause undefined behavior; Thus it can overwrite data, but won't necessarily.

To correct this issue, you can make the Student struct have a list attribute, and since you are inputing floats, it should be a list of floats. Then, you also need to correct the indices used in the first for loop since, as explained above, they do not perform the intended behavior.

Something like this should work :

#include <stdio.h>
#define NUM_SUBJECTS 3
// Defining a structure for student details
struct Student {
    float marks[NUM_SUBJECTS];
};

int main() {
    struct Student st[2];

    // Input details for 2 student marks
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < NUM_SUBJECTS; j++) {
            printf("Marks: ");
            scanf("%f", &st[i].marks[j]);
        }
    }

    for (int i = 0; i < 2; ++i) {
        printf("Marks for student %d:\n", i + 1);
        for (int j = 0; j < NUM_SUBJECTS; j++) {
            printf("Subject %d: %f\n", j + 1, st[i].marks[j]);
        }
    }

    return 0;
}


Here the define is a C macro which allows you to change the number of subject at only one place.

Sign up to request clarification or add additional context in comments.

8 Comments

Okay I just wanted to clear my confusion but I've just runned the code and it does not overwrite the data I can successfully get and print it but as usual beginner Shouldn't it be overwriting since we have not made an array to store for float m[6]; ?. I pasted this same thing here expecting you'll clear it.
@AlsiroMira Each student has an array of length 3. you have an array of length 2 of students. So in total you have 6 different memory places to store your marks
@AlsiroMira In your opinion, what does &st[i].marks[j] do ?
provides a like pointer the memory location for the mark of j-th subject of the i-th student. Still the confusion why it is not over writing ?.
@AlsiroMira Each student is like a house. Each house have a "floor" for each mark. &st[i].marks[j] is looking at the floor j of the house i. You have 6 different floor addresses : house 1 floor 1, house 1 floor 2, house 1 floor 3; house 2 floor 1, house 2 floor 2, house 2 floor 3.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.