Passing structures to functions in C - c

Define a struct named Book. Each book should have a name with
exactly 3 letters (abbreviation). Each book should also have a page
count (integer), and a price (integer).
Write a program which reads an integer n first, then reads the
names, page counts and prices of n books.
Write a function which takes an array of books, and sorts them
according to their prices. Using that function, your program should
print the names and page counts of each book with the order of their
prices.
My Question
Can someone explain to me how to pass a structure into a function and get this code to work? Or how they would go tackle this question.
struct Book{
char name[3];
int pagec;
int price;
};
void price(int size, struct Book books[size]){
int i,j, tmp;
for(i=0; i<size; i++){
for(j=0; j<size-1; j++){
if(books[j].price < books[j+1].price){
books[j].price = tmp;
books[j].price = books.price[j+1];
books.price[j+1] = tmp;
}
}
}
}
int main(void) {
int n;
scanf("%d", &n);
struct Book books[n];
int i,j;
for(i=0; i<n; i++){
for(j=0; i<1; j++){
scanf("%c", &books[i].name);
scanf("%d", &books[i].pagec);
scanf("%d", &books[i].price);
}
}
price(n, books[n]);
for(i=0; i<n; i++){
printf("%c: %d - %d",books[i].name, books[i].pagec, books[i].price);
}

The part of your question about how to pass a struct seems to have been well answered, let me get on the second part: how I would tackle that question.
What the struct should be is described well, so we can write without much thinking:
/*
Define a struct named: Book.
Each book should have a name with exactly 3 letters (abbreviation).
Each book should also have a page count (integer),
and a price (integer).
*/
typedef struct book {
// three characters plus '\0'
char name[4];
int pagec;
int price;
} book_t;
(No need for a typedef you can leave that part out and use the struct directly with struct books)
They want three functions, one is main() we can use it as one of the three. I think getting the info, allocating the memory etc. is a good use of the main() function here.
/*
Write a program which
reads an integer n first,
then reads the names,
page counts
and prices of n books.
*/
int main()
{
int n, i;
book_t **books;
puts("How many books?");
scanf("%d", &n);
// we need enough memory to safe "n" structs
// at first allocate memory for n pointers
books = malloc(n * sizeof(book_t *));
// at each pointer allocate enough memory for one struct books
for (i = 0; i < n; i++) {
books[i] = malloc(sizeof(book_t));
}
// red the info from stdin
for (i = 0; i < n; i++) {
printf("Name of book (3 letter abbrev.):");
scanf("%3s", books[i]->name);
printf("Number of pages:");
scanf("%d", &books[i]->pagec);
printf("Price of book (incl. taxes):");
scanf("%d", &books[i]->price);
}
// call function to sort them
sort_books(books, n);
// call a function to print the sorted list
print_books(books, n);
// we don't need the memory anymore, so free it
// at first free the individual structs
for (i = 0; i < n; i++) {
free(books[i]);
}
// then free the memory holding all of the pointers
free(books);
exit(EXIT_SUCCESS);
}
The two functions for printing and sorting are similar in argument handling
/*
Write a function which takes an array of books,
and sorts them according to their prices.
Doesn't say something about output, so sort in situ
*/
void sort_books(book_t ** books, int length)
{
// OP had bubble sort, so lets do a bubble sort. Why not?
int i, j;
book_t *tmp;
for (i = 0; i < length - 1; i++) {
for (j = 0; j < length -i - 1; j++) {
if (books[j]->price < books[j + 1]->price) {
tmp = books[j];
books[j] = books[j + 1];
books[j + 1] = tmp;
}
}
}
}
Printing them all is quite straightforward
/*
Using that function, your program should
print the names and page counts of each book with the order of their prices.
*/
void print_books(book_t ** books, int length)
{
int i;
for (i = 0; i < length; i++) {
printf("Name %s, pages %d, price %d\n",
books[i]->name, books[i]->pagec, books[i]->price);
}
}
It doesn't say anything about the exact order, I took the liberty to use a descending order (from highest to lowest). If you want an ascending order (from lowest to highest) change the comparing in the sorting algorithm:
void sort_books(book_t ** books, int length)
{
// OP had bubble sort, so lets do a bubble sort
int i, j;
book_t *tmp;
for (i = 0; i < length - 1; i++) {
for (j = 0; j < length - i - 1; j++) {
// if (books[j]->price < books[j + 1]->price) {
if (books[j]->price > books[j + 1]->price) {
tmp = books[j];
books[j] = books[j + 1];
books[j + 1] = tmp;
}
}
}
}
Please be aware that I omitted all checks! You need to check the returns of malloc() and scanf(), if n is an integer, if all of the other numbers are numbers and so on!

books[n] is one struct Book (or would be, if the array had n+1 elements).
The name of the array is just books, and this is what you should pass to the function:
price(n, books);

So you have your struct
struct Book{
char name[10];
int pagec;
int price;
};
You can pass it in by passing in a pointer to it using the "address of" operator
void receivingFunction(Book* myBook)
{
printf("%s", myBook->name);
}
void sendingFunction()
{
Book myBook;
//set values in myBook
receivingFunction(&myBook);
}
Notice that when you are working with the pointer to book, you access members using the -> operator, not the . operator.
Now that example above is just for passing in a single instance. What if you want to pass in an array? It'll look something like this.
#include <stdio.h>
#include <string.h>
struct Book{
char name[4]; //other answers explain well why I changed this to 4
int pagec;
int price;
};
void BookSorter(struct Book books[10], int booksLength)
{
int i;
for(i = 0; i < booksLength; i++)
{
printf("%s %d %d\n", books[i].name, books[i].pagec, books[i].price);
}
}
int main(void)
{
Book books[10];
//define your values for books here
//mine are junk values since this is just an example
for(int i = 0; i < 10; i++)
{
strncpy(books[i].name, "aaa", 4);
books[i].pagec = 4;
books[i].price = 10;
}
//
BookSorter(books, 10);
}
From here you can modify your bubble sort to iterate through your array instances and swap them. I'm not going to include that part because 1) it is beyond the scope of your original question about passing structs and 2) it really looks like you're doing homework and I don't want to give you all of it. The comments below your question address ways to fix your bubble sort, one I haven't seen yet is that you're just swapping the prices of the books, not the books themselves. Your temp variable should be a struct Book, not an int. Your swapping just swaps the prices of the books, which will lead to them being assigned to the wrong book for the final printing out of the answer, with the books and page counts being left in the same order they were read in. It looks like you used this example code (or maybe one of a million like it), but here it is if you need a reference for implementing bubble sort.

Related

qsort in C give wrong results

I am trying to sort an array of structs (A SJF Scheduler). I am using the qsort library function to sort the structs in increasing order according to the attribute bursttime. However, the output is not correct. I've checked some SO questions about the same but they served no use.
struct job
{
int jobno;
int bursttime;
};
typedef struct job job_t;
int mycompare(const void* first, const void* second)
{
int fb = ((job_t*)first)->bursttime;
int sb = ((job_t*)second)->bursttime;
return (fb - sb);
}
int main()
{
int n;
printf("Enter number of jobs: ");
scanf("%d", &n);
job_t* arr = (job_t*)malloc(sizeof(job_t) * n);
for(int i = 1; i <= n; ++i)
{
printf("Enter Burst time for Job#%d: ",i);
scanf("%d", &(arr[i].bursttime));
arr[i].jobno = i;
}
printf("\n");
printf("Order of the Jobs before sort:\n");
for(int i = 1; i <= n; ++i)
{
printf("%d\t", arr[i].jobno);
}
qsort(arr, n, sizeof(job_t), mycompare);
printf("\n");
printf("Order of the Jobs after sort:\n");
for(int i = 1; i <= n; ++i)
{
printf("%d\t", arr[i].jobno);
}
printf("\n");
printf("\n");
return 0;
}
This is my inputfile:
4
7
2
9
4
The output I'm getting is:
Order of the Jobs before sort:
1 2 3 4
Order of the Jobs after sort:
2 1 3 4
The expected order should be: 2,4,1,3. Am I missing anything?
At least this problem
for(int i = 1; i <= n; ++i) // bad
Use zero base indexing.
for(int i = 0; i < n; ++i)
You could change the indexing scheme to start from zero, as others have suggested, and this would certainly be the idiomatic way to do it.
But if you want to use 1-based indexing, you'll need to allocate an extra place in the array (position 0 that will never be used):
job_t* arr = (job_t*)malloc(sizeof(job_t) * (n + 1));
Then you'll need to start your sort at position 1 in the array:
qsort(&arr[1], n, sizeof(job_t), mycompare);
And, of course, you'll have to write your code to index from 1 -- but you've already done that.
The problem is that so many standard functions in C use zero-based indexing that doing anything else is inexpressive. That's a bigger problem than wasting one array position. But, for better or worse, I've had to convert to C a load of code from Fortran, so I've gotten used to working both ways.

Write a function that finds the top 5 max values in array of structures?

Find the top 5 maximum values in array of structures (for C programming)?
I have an array of structures as follows:
struct info {
char name[100];
int number;
}
struct info people[10]
In char name[100] are names of people (up to 10) and they have a corresponding value in int balance:
Jane 10
John 40
Harry 16
Eva -5
...
until it gets to 10 people.
How do I find and print the 5 people who have the highest numbers?
i.e:
John 40
Harry 16
Jane 10
...
I have tried the following code:
int i,j, k=5, max, temp;
//move maximum 5 numbers to the front of the array
for (i=0; i<k; i++) {
max=i;
for (j=i+1; j<10; j++) {
if (people[i].number>people[max].number) {
max=j;
}
}
//swap numbers
temp=people[i].number;
people[i].number=people[max].number;
people[max].number=temp;
//swap names to match swapped numbers so they correspond
temp=people[i].name;
people[i].name=people[max].name;
people[max]=temp;
}
for (i=0; i<k; i++) {
printf("%s %d\n", people[i].name, people[i].number);
}
However, I get an error message on the second swap since its char type. How should I fix this or what else would work for this objective?
Just sort the array and then take the 5 first/last (depending on the sort order) entries of the sorted array.
1st define a compare function:
#include <stdlib.h> /* for qsort() */
#include <stdio.h> /* for printf() */
struct info
{
char name[100];
int number;
};
int cmp_struct_info_desc(const void * pv1, const void * pv2)
{
const struct info * pi1 = pv1;
const struct info * pi2 = pv2;
return pi2->number - pi1->number;
}
2ndly use the Standard C function qsort().
struct info people[] = {
... /* initialise array people here ... */
}
int main(void)
{
size_t number_of_array_elements = sizeof people/sizeof *people;
qsort(people, number_of_array_elements, sizeof *people, cmp_struct_info_desc);
for (size_t s = 0; s < number_of_array_elements; ++s)
{
printf("%zu. = {%d, '%s'}\n", s, people[s].number, people[s].name);
}
}
Starting from OP's code, just swap the structure. #BLUEPIXY. Only a small changed needed for OP's code.
Arrays can not be copied with assignments but objects, like struct info can be assigned.
int i, j, k=5;
//move maximum `k` numbers to the front of the array
for (i=0; i<k; i++) {
int max=i;
for (j=i+1; j<10; j++) {
if (people[i].number > people[max].number) {
max=j;
}
}
//swap info
struct info temp = people[i];
people[i] = people[max];
people[max] = temp;
}
for (i=0; i<k; i++) {
printf("%s %d\n", people[i].name, people[i].number);
}
The easiest and most general way would probably be to first sort the people array. Once this is done just pick the first five elements.
Best way is to sort the array people based on the number attribute iso individual swaps.
If you want to go ahead with current approach use strcpy functions iso "=" operator for name
Rather than keeping track of 5 out of total 10 indices, sorting seems better to me.
You can use qsort to sort the 10 elements and then pick the top 5.
int cmp(void *a,void *b)
{
struct info as=*((struct info*)a);
struct info bs=*((struct info*)b)
return bs.number-as.number; //sorting in descending order
}
and then
qsort(people,10,sizeof people[0],cmp);

How to read in 2d array from function in main

Alright so I'm sure this is pretty simple and all but I have no idea how to use functions. Up until now I was able to get by with everything in main but now I'm required to use functions for just about anything I do. So from my code below, how do I read in (or w.e. the proper terminology is) a function from main?
EDIT: To clarify to everyone, my question is how can I access the array I returned in main?
Code below takes in the test scores from different amount of students specified by user input.
#include <stdio.h>
#include <stdlib.h>
int** getTestData();
int main (){
///this is where I'm lost..
int (*a)[];
a = getTestData();
}
int** getTestData(){
int students, numberOfTests, testScores, i, j;
int** testScoreBank;
// reads in studens
scanf("%i", &students);
testScoreBank = (int**) malloc (sizeof(int)*students);
for(i=0; i<students;i++){
//how many number of tests there are
scanf("%i", &numberOfTests);
testScoreBank = (int*) malloc (sizeof(int)*numberOfTests);
for(j=0; j<numberOfTests; j++){
//the tests themselves
scanf("%i", &testScores);
testScoreBank[i][j] = testScores;
}
}
return testScoreBank;
}
Ok, here is an example with global variables, how to fill your array inside a function, and access it in main()
#include <stdio.h>
#include <stdlib.h>
int** getTestData();
int numberOfStudents;
int* studentTestSizes;
int main (){
int** testScoresBank = getTestData();
int i, j;
for (i = 0; i < numberOfStudents; i++) {
for (j = 0; j < studentTestSizes[i]; j++) {
printf("%d", testScoresBank[i][j]);
}
}
return 0;
}
int** getTestData() {
int** testScoreBank;
// reads in studens
scanf("%d", &numberOfStudents);
testScoreBank = malloc(sizeof(int*) * numberOfStudents);
studentTestSizes = malloc(sizeof(int) * numberOfStudents);
int i;
for (i = 0; i < numberOfStudents; i++) {
//how many number of tests there are
scanf("%d", studentTestSizes + i);
testScoreBank[i] = malloc(sizeof(int) * studentTestSizes[i]);
int j;
for (j = 0; j < studentTestSizes[i]; j++) {
//the tests themselves
int testScore;
scanf("%d", &testScore);
testScoreBank[i][j] = testScore;
}
}
return testScoreBank;
}
alternative for global variables is to make global variables local and pass pointers to them to getTestData function, example is here:
#include <stdio.h>
#include <stdlib.h>
int** getTestData();
int main (){
int numberOfStudents; // those variables are now here
int* studentTestSizes;
int** testScoresBank = getTestData(&numberOfStudents, &studentTestSizes); // passing the pointers so we can change values that are pointed to
int i, j;
for (i = 0; i < numberOfStudents; i++) {
for (j = 0; j < studentTestSizes[i]; j++) {
printf("%d", testScoresBank[i][j]);
}
}
return 0;
}
int** getTestData(int* numberOfStudentsPtr, int** studentTestSizesPtr) {
int** testScoreBank;
// reads in studens
scanf("%d", numberOfStudentsPtr); // it's already a pointer so we must omit &
int numberOfStudents = *numberOfStudentsPtr; // will be constant from now on
testScoreBank = malloc(sizeof(int*) * numberOfStudents);
*studentTestSizesPtr = malloc(sizeof(int) * numberOfStudents);
int* studentTestSizes = *studentTestSizesPtr;
int i;
for (i = 0; i < numberOfStudents; i++) {
//how many number of tests there are
scanf("%d", studentTestSizes + i);
testScoreBank[i] = malloc(sizeof(int) * studentTestSizes[i]);
int j;
for (j = 0; j < studentTestSizes[i]; j++) {
//the tests themselves
int testScore;
scanf("%d", &testScore);
testScoreBank[i][j] = testScore;
}
}
return testScoreBank;
}
In addition to the other answers, you have a number of subtle issues to consider. Two are just general helpful tips for writing/debugging your beginning applications (1) if you are taking user input, prompt for it - you can strip the prompts later, but it is much easier to enter data in response to an informative prompt than it is to wonder what the blinking cursor is doing down there -- did to program freeze?; (2) provide adequate spacing in your code - you can always delete blank lines later, but separating your code into functional blocks of logic will help you keep your logic straight.
When allocating space for numeric arrays, it is good practice to initialize all values to 0 as part of (or after) allocation. This will absolutely prevent the inadvertent read from uninitialized space. You can allocate and initialize all at once by using calloc instead of malloc. This also provides benefits when allocating arrays of pointers to char* as well.
While you may have fixed your allocation, you still have the glaring problem of knowing "How many students and tests do I have?". Here is where passing an additional pointer for the number of students and storing the number of tests in an additional array index is required. (there are other ways to do it, this is just efficient). You can declare the the number of students in main passing its pointer to getTestData. Updates to the value of students in getTestData are then available in main. But, "What if the students have a different number of scores? What then?" You are already filling an array of integers, if you just store the number of tests for the student as the first integer, you make that value available no matter where you pass the array.
Finally, you need to pay closer attention to your choice of variable types. For indexes and lengths that can never be negative, unsigned or size_t is a better choice and will allow the compiler to point out instances where you may be using that value incorrectly.
You need to validate each time you allocate memory with malloc or calloc, and you are responsible to keeping track of the address to the start of each allocation and freeing it when it is no longer needed.
That said, here is one approach to making it all work. Try it. Let me know if you have any questions:
#include <stdio.h>
#include <stdlib.h>
int **getTestData (size_t *students);
int main (void) {
int **a = NULL;
int j;
size_t i, s = 0;
if (!(a = getTestData (&s))) {
fprintf (stderr, "error: getTestData failed to return student data.\n");
return 1;
}
/* print student data */
putchar ('\n');
for (i = 0; i < s; i++)
{
printf (" Student[%2zu] scores : ", i+1);
/* adjust indexes to read no. of tests */
for (j = 1; j < a[i][0]; j++)
printf (" %3d", a[i][j]);
putchar ('\n');
}
putchar ('\n');
/* free allocated memory */
for (i = 0; i < s; i++)
free (a[i]);
free (a);
return 0;
}
int **getTestData (size_t *students)
{
size_t tests, testScores, s, t;
int **testScoreBank;
/* reads in students */
printf ("\n No. of students: ");
scanf ("%zu", students);
if (!(testScoreBank = calloc (*students, sizeof *testScoreBank))) {
fprintf (stderr, "%s() error: virtual memory exhausted.\n", __func__);
return NULL;
}
for (s = 0; s < *students; s++)
{
/* how many number of tests there are */
printf ("\n No. of scores for student[%2zu]: ", s+1);
scanf ("%zu", &tests);
tests += 1; /* allow space for number of tests as [s][0] */
testScoreBank[s] = calloc (tests, sizeof **testScoreBank);
testScoreBank[s][0] = tests;
for (t = 1; t < tests; t++)
{
/* the tests themselves */
printf (" student[%2zu]-test[%2zu] score: ", s+1, t);
scanf ("%zu", &testScores);
testScoreBank[s][t] = testScores;
}
}
return testScoreBank;
}
Use/Output
$ ./bin/testdata
No. of students: 2
No. of scores for student[ 1]: 3
student[ 1]-test[ 1] score: 88
student[ 1]-test[ 2] score: 91
student[ 1]-test[ 3] score: 82
No. of scores for student[ 2]: 4
student[ 2]-test[ 1] score: 93
student[ 2]-test[ 2] score: 95
student[ 2]-test[ 3] score: 96
student[ 2]-test[ 4] score: 91
Student[ 1] scores : 88 91 82
Student[ 2] scores : 93 95 96 91
(I assume this is just a snippet of your overall code and there's more to it or else you might have other problems)
Anyway you should be able to set up your 2D array with the following modifications:
scanf("%i", &students);
testScoreBank = malloc (sizeof(int*)*students); //sizeof int* instead of sizeof int.
for(i=0; i<students;i++)
{
scanf("%i", &numberOfTests);
*(testScoreBank + i)= malloc (sizeof(int)*numberOfTests); //You forgot the * operator on testScoreBank and to iterate through it.
for(j=0; j<numberOfTests; j++)
{
scanf("%i", &testScores);
testScoreBank[i][j] = testScores;
}
}

Need help creating a parallel array in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Just a disclaimer: I am a complete noob when it comes to C programming and this might be embarrassingly easy but I can't think it out myself and haven't found anything otherwise helpful. Anyways, this program below is supposed to put names (last, first) and ages into arrays and then alphabetize them. The problem I have is creating a sort of parallel array to match ages with their respective names after sorting them alphabetically. Does anyone have any suggestions? I was thinking of somehow tying the age array into my defined functions, but I still can't wrap my head around that either. Thanks in advance!
#include <stdio.h>
#include <string.h>
#define NAME_LENGTH 50
#define MAX_NAMES 50
int alpha_first(char *list[], int min_sub, int max_sub);
void select_sort_str(char *list[], int n);
int
main(void)
{
int i = 0;
int numpeople;
char names[MAX_NAMES][NAME_LENGTH] = { 0 };
char ages[MAX_NAMES][NAME_LENGTH] = { 0 };
char skip_line;
char *alpha[MAX_NAMES] = { 0 };
printf("Enter number of people <0..50>\n> ");
scanf("%d", &numpeople);
do
{
scanf("%c", &skip_line);
}
while (skip_line != '\n');
for(i = 0; i < numpeople; ++i)
{
printf("Enter name %d (lastname, firstname) ", i+1);
gets(names[i]);
printf("Enter age %d: ", i+1);
gets(ages[i]);
}
for(i = 0; i < numpeople; ++i)
{
alpha[i] = names[i];
select_sort_str(alpha, numpeople);
}
printf("\n\n%-30s\n\n", "Original List");
for (i = 0; i < numpeople; ++i)
printf("%-30s\n", names[i]);
printf("\n\n%-30s\n\n", "Alphabetical Order");
for (i = 0; i < numpeople; ++i)
printf("%-30s\n", alpha[i]);
return 0;
}
int
alpha_first(char *list[],
int min_sub, int max_sub)
{
int first, i;
first = min_sub;
for (i = min_sub + 1; i <= max_sub; ++i)
if (strcmp(list[i], list[first]) < 0)
first = i;
return (first);
}
void
select_sort_str(char *list[], int n)
{
int fill, index_of_min;
char *temp;
for (fill=0; fill<n-1; ++fill)
{
index_of_min = alpha_first(list, fill, n - 1);
if (index_of_min != fill)
{
temp = list[index_of_min];
list[index_of_min] = list[fill];
list[fill] = temp;
}
}
}
Use a structure:
typedef struct
{
char *first_name;
char *last_name;
int age;
} person;
When you create your array, create an array of person; Create a new person, fill in its fields, add it to your array. Now you have all of the data you need in one nice little package.
Most problems in computer science are solved by introducing an extra level of indirection.
In this case, observe that select_sort_str() is sorting an array of pointers to strings. Suppose instead you construct an array of index values, initialised to 0..numpeople-1, and then you sort those indexes into the order of the corresponding names. So, in alpha_first(), as well as taking char* list[] let it also take int index[] and the comparison becomes:
if (strcmp(list[index[i]], list[index[first]]) < 0)
and similarly select_sort_str(), in which you don't swap the entries in the list[] but in the index[]. Then to read the entries in alpha order, you use the extra level of indirection:
names[index[i]]

Using Typedef to store elements in a 2D Array

I'm changing a program I've already completed (that uses 4 arrays) into a new program that uses typedef to create a structure that holds those arrays, so I'm only using one instead.
To do this, I used this code:
typedef struct structure
{
char names[13][9];
int scores[13][4];
float average[13];
char letter[13];
} stuff;
The one array that now contains all of these is:
stuff everything[13];
However I'm running into a few complications since 'names' 'scores' 'average' and 'letter' no longer exist, I have to change it to go through the typedef instead. So, as an example, I have this code:
for(i=0; i<13; i++)
{
for(j=0; j<4; j++)
{
fscanf(score, "%d", &scores[i][j]);
}
}
fclose(score);
How would I get it to store the information in the 'scores' array still?
Also how would I later call the information out of that array?
You still have scores, it's just inside the structure. You can access it using normal array and structure access operators:
everything[x].scores[i][j] = 6;
x has to be between 0 and 12 (inclusive) as you declared everything to be 13 of stuff.
stuff everything[13];
declares an array of 13 structs with type stuff. It is equivalent to
struct structure everything[13];
with typedef, you don't need to write struct structure again.
In order to access information from the above array of structs, you may do something like the following for scores (similar reasoning apply to other fields:
for (int i = 0; i < 13; ++i )
{
for ( int j = 0; j < 13; ++j)
{
for (int k = 0; k < 4; ++k)
{
printf("%d ", everything[i].scores[j][k]);
}
}
}
When you want to put stuff into this array of structs, you should follow the same logic as above.
Possibly what you want is something like this:
#define NUM_TEAMS 13
typedef struct
{
char names[9];
int scores[4];
float average;
char letter;
} team;
team everything[NUM_TEAMS];
int readInScores(team* allTeams)
{
int i = 0;
int j= 0;
FILE* score = fopen("whatever", "r");
if (score == 0)
return 0;
for(i=0; i<NUM_TEAMS; i++)
{
for(j=0; j<4; j++)
{
fscanf(score, "%d",&allTeams[i].scores[j]);
}
}
fclose(score);
return 1;
}

Resources