Hello. I'm having a bit of trouble with an assignment where I'm supposed to pass arrays of structures to a function using a pointer.
This is where I'm at right now:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define itemNum 4
#define INPUT1 "INPUT1.txt"
#define INPUT2 "INPUT2.txt"
struct item
{
int itemCode;
char item[20];
int median;
};
struct storage
{
int itemCode;
int storageState;
};
void readFile(struct item *pStr[itemNum], struct storage *tStr[itemNum]);
int main(void)
{
struct tooted p[tooteArv];
struct laoseis t[tooteArv];
readFile(&p, &t);
<more stuff here>
return 0;
}
void readFile(struct item *pStr[itemNum], struct storage *tStr[itemNum])
{
int i;
FILE *fi1, *fi2;
fi1 = fopen(INPUT1, "r");
if(!fi1)
{
printf("INPUT FILE ERROR\n");
exit(1);
}
for(i = 0; i < itemNum; i++)
{
fscanf(fi1, "%d", &pStr[i]->itemCode);
fscanf(fi1, "%s", pStr[i]->item);
fscanf(fi1, "%d", &pStr[i]->median);
}
fclose(fi1);
fi2 = fopen(INPUT2, "r");
if(!fi2)
{
printf("INPUT FILE ERROR\n");
exit(1);
}
for(i = 0; i < itemNum; i++)
{
fscanf(fi2, "%d", &tStr[i]->itemCode);
fscanf(fi2, "%d", &tStr[i]->storageState);
}
fclose(fi2);
}
I'm aware this might look completely wrong at current time, since the pointers were not applied in the main function, but I couldn't figure out how to apply them to a structure array.
I'm looking for help on how to correctly point to a structure array, pass it into a function via the pointer and apply values to the corresponding structure members from within the function.
Thanks!
Small mistakes may be included, for the variables were translated from another language than the chunk was originally written in.