I have the following code:
#include<stdio.h>
struct student {
int* grades;
int num_grades;
};
double* get_means(struct student* arr, int n); // Function Signature
// grade range is: 0 - 100
// assume max number of grades to one student is up to 100
double* get_means(struct student* arr, int n)
{
arr->grades = 90;
printf("%d", arr->grades);
}
int main()
{
struct student s, *p;
s.grades = malloc(s.num_grades * (sizeof(int)));
p->grades[0] = 1;
printf("%d", p->grades[0]);
}
and I'm having an issue assigning a value (it doesn't matter what the value is, it can be: 0, 7, 50).
The compiler gives me the error:
Error C4700 uninitialized local variable 's' used
Error C4700 uninitialized local variable 'p' used
What can I do to fix this?
In other words: how do I assign a value to an array in a struct?
s.num_grades- this is definitely not initialized. And you need to includestdlib.hs.num_gradesin the argument tomalloc, but you never gave it a value. It is undefined. Either don't use it, or else assign something to it first. (2) You're assigning top->grades[0]without settingpfirst. So setpto some valid pointer value first. If you don't understand what pointers are, then take a step back and find a C tutorial to learn about them.arr->grades = 90;is wrong.gradesis a pointer to an array of grades, not a single grade.p = &s?