# Thread: Accessing a struct problem

1. ## Accessing a struct problem

Hey guys. I put together this program that is suppose to count up and average the grades of a class of 5 people. In it, I have
'A'=4,'B'=3, and so on. I wrote a function that I thought would average them out, but it keeps coming up with 0.00000. just like that. Can anyone see something that I am doing wrong. possibly way wrong? here is the code in the .h file:

Code:
```#include <stdio.h>

#define CLASS_SIZE  100

struct student{
char	*last_name;
int	student_id;
};```

in the .c file:

Code:
```#include "cl_info.h"

void average(struct student class[]);

int main(void)
{
struct student	temp, class[CLASS_SIZE];

temp.last_name = "Bushker";
temp.student_id = 590017;

temp.last_name = "Donald";
temp.student_id = 590117;

temp.last_name = "Trump";
temp.student_id = 590217;

temp.last_name = "Lewis";
temp.student_id = 590317;

temp.last_name = "Smith";
temp.student_id = 590417;

average(class);
return 0;
}

void average(struct student class[])
{
double sum=0.0,avg;
int i;

for(i=0;i<5;++i){
sum+=4;
sum+=3;
sum+=2;
sum+=1;
sum+=0;

}
avg = (sum/5);
printf("The average for the class is %f.\n", avg);
}```
any help is greatly appreciated.

2. 'temp' is a single structure. You keep overwriting it.

Quzah.

3. should I use something like temp, temp1,temp2 and so on
would that work?

4. Why don't you just use the array you made?

Quzah.

5. i don't think I understand. I am totally new to structures.

6. Code:
`void average(struct student class[])`
Legal C? And how do you go about knowing about structures, pointers, arrays, functions, and still not understand what Quzah told you.

EDIT: Mmmkay I guess it is legal C. Didn't think you could pass arrays to functions like that in C.

7. See your function average? See that array of structures you're passing to it? See how you access the 'grade' portion of the structure there? Do the same thing, but assign values to the array, instead of reading it from them:
Code:
```class[0].grade = 'A';
class[0].last_name = "Bushker";
class[0].student_id = 590017;```
And so on...

Quzah.

8. Originally Posted by SlyMaelstrom
Code:
`void average(struct student class[])`
Legal C?
Sure, what wouldn't be legal about it?

Quzah.

9. i'm sorry guys. I just read a new chapter about structures, and I am just trying to understand how to use them.

if I have:

Code:
```int main(void)
{
struct student    temp, class[CLASS_SIZE];

temp.last_name = "Bushker";
temp.student_id = 590017;```

i just don't understand what exactly is in the array class.
are the members in there. Aren't these values I've assigned in the structure variable temp. Please guys, just an explanation.

10. Originally Posted by quzah
See your function average? See that array of structures you're passing to it? See how you access the 'grade' portion of the structure there? Do the same thing, but assign values to the array, instead of reading it from them:
Code:
```class[0].grade = 'A';
class[0].last_name = "Bushker";
class[0].student_id = 590017;```
And so on...

Quzah.
Or use a for loop and an array of strings:
Code:
`const char *names[] = {"Joe", "Sam", "Mike"};`

11. You have declared two things:
Code:
```struct student temp; /* One single student, not connected in any way to anything else. */
struct student class[ SOMESIZE ]; /* An array of 'struct student's. */```
This is just the same as any other variable declaration:
Code:
```int temp; /* One single integer, not connected to anything else in any way. */
int class[ SOMESIZE ]; /* An array of integers. */```
In the second code fragment, you wouldn't expect that when you change 'temp' it effects the array would you? So why would you expect it to do so with structures?

Quzah.

12. That worked great. THANK YOU QUZAH!!! My book didn't tell me that the
class[i].grade is what you use to assign values to the members. I really appreciate it, man.
I also see now how it is suppose to work.

13. I can see now how they all mesh with each other. I have no idea why I thought temp would take me where I needed to go. brainfart, i guess. thanks again!

14. Originally Posted by Hoser83
i'm sorry guys. I just read a new chapter about structures, and I am just trying to understand how to use them.

if I have:

Code:
```int main(void)
{
struct student    temp, class[CLASS_SIZE];