I want to pass a struct array into a function and I keep getting an error. I'm not sure how to do it in general as I was never taught. I've done some googling but I can't find anything. Can you guys help me out and explain why this is not working? Here's the code of the entire program (it's small, just a test program):
#include <stdio.h>
#include <stdlib.h>
#define hour 60
int getNum(void);
int distance(int start, int end , int flight_time[5], int flight_layover[5]);
int main(void)
{
int user_start=-1;
int user_end=-1;
int travel_time=0;
struct flight
{
int flight_time;
int flight_layover;
};
struct flight flights[5]=
{
{4 * hour + 15, 1 * hour + 20},
{3 * hour + 58, 0 * hour + 46},
{3 * hour + 55, 11 * hour + 29},
{2 * hour + 14, 0 * hour + 53},
{3 * hour + 27, 0 * hour + 0}
};
printf ("Hello sir. Please enter you starting city:\n");
user_start=getNum();
user_start--;
printf ("Good. Now enter the city you would like to end it:\n");
user_end=getNum();
user_end--;
travel_time = distance(user_start,user_end, flights[5].flight_layover, flights[5].flight_time);
printf ("The total travel time from %d to %d is %d.",user_start, user_end, travel_time);
return 0;
}
int distance(int start, int end , int flight_time[5], int flight_layover[5])
{
int total_mins=0;
int i=0;
for (i=end+1;i--;i=start)
{
total_mins=total_mins + flight_time[i] + flight_layover[i];
}
return total_mins;
}
int getNum(void)
{
char record[121] = {0};
int number = 0;
fgets(record, 121, stdin);
if(sscanf_s(record, "%d", &number) != 1 )
{
number = -1;
}
return number;
}
int flight_time[5]is exactly equivalent toint *flight_time; the5is ignored. Suggested reading: section 6 of the comp.lang.c FAQ.