1

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?

Flux
11.1k6 gold badges64 silver badges112 bronze badges
asked Jun 23, 2020 at 15:53
6
  • s.num_grades - this is definitely not initialized. And you need to include stdlib.h Commented Jun 23, 2020 at 15:58
  • 1
    (1) You're using s.num_grades in the argument to malloc, 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 to p->grades[0] without setting p first. So set p to 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. Commented Jun 23, 2020 at 16:00
  • You never returned anything from the user-defined function. Commented Jun 23, 2020 at 16:31
  • arr->grades = 90; is wrong. grades is a pointer to an array of grades, not a single grade. Commented Jun 23, 2020 at 16:46
  • Maybe you intended to do p = &s? Commented Jun 23, 2020 at 16:48

1 Answer 1

1

When you try to do that p->grades[0] = 1 first you need to assign the address for s to p and only than trying to access s from the pointer p. Second issue here is you trying to access array only through is name (in the function get means), As you know array name its only the base address of the array its not the value in it. Third issue its the function does not return nothing but in the prototype it returns pointer to double, I suggest you to change it to avoid any unnecessary warnings or errors. And another suggestion is including <stdlib.h> when using malloc and always check malloc does not returns you NULL.

The fixed code attached:

 struct student {
 int* grades; 
 int num_grades;
 };
void get_means(struct student* arr, int n); 
void get_means(struct student* arr, int n)
{
 arr->grades[0] = 90;
 printf("%d", arr->grades[0]);
}
int main()
{
 struct student s, *p;
 s.grades = malloc(s.num_grades * (sizeof(int)));
 /* Option 1 
 s.grades[0] = 1;
 printf("%d", s.grades[0]);
 */
 
 /* Option 2 */
 p = &s;
 p->grades[0] = 1;
 printf("%d", p->grades[0]);
 }
answered Jun 23, 2020 at 17:11
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.