I have code to store data into this struct:
typedef struct sales{
char *date;
char *stockCode;
int quantity;
} sales;
I do it via this function:
void storeSales(FILE *salesf){
struct sales sale[40000];
char str[255];
// counter for array position
int counter = 0;
while (fgets(str, 255, salesf) != NULL){
counter++;
char *date = strtok(str, " ,");
char *stockCode = strtok(NULL, " ,");
// store the
sale[counter].date = malloc(strlen(date)+1);
strcpy(sale[counter].date, date);
sale[counter].stockCode = malloc(strlen(stockCode)+1);
strcpy(sale[counter].stockCode, stockCode);
sale[counter].quantity = atoi(strtok(NULL, "\n"));
}
}
But now the values are stored I want to use them. I could of course add this to the storeSales(FILE *salesf)
function to print them:
int count = 0;
for(count =0; count <counter; count++){
printf("%s %s %d \n", sale[count].date, sale[count].stockCode, sale[count].quantity);
}
But of course having a function to store the data which then prints them isn't quite what I want.
Within my main I wish to have access to this array of structs, I've read you cannot pass back an array of structs in a function.
Is there a way to return an array of pointers which are pointing to the structs?
It's all very new to me!
I'm trying to make my code as modular as possible.