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;
}
Your
distancefunction is all wrong. Since you have astructthat containsflight_timeandflight_layover, you want to pass an array of thesestructsto the function, not an array of each of thoseintvalues. ie.Changed the function signature and the line inside the
forloop.Then where you call
distance, you can change the call to:That’ll pass a pointer to the start of your array (
flights), anduser_startanduser_endwill specify the bounds of the array to be used to calculate the distance.Also note that you could be accessing indexes out of the
flightsarray’s bounds. I especially don’t understand why you havei=end+1, perhaps you wantedi = end - 1?You also have the condition and decrement part of your for loop reversed, this is how I think it should be:
EDIT: Obviously your function prototype needs to be updated, and your
structs should be declared before they’re used anywhere (including before the prototypes). Here’s the whole code with the modifications I’ve mentioned.Also, be careful with your
getNumfunction, since it can return -1 upon error and you’ll cause undefined behaviour if you’re accessing elements before your array. You should also check the user input to ensure that the start and end values are between 1 and 5, and are decremented so that they’re valid array indexes between 0 and 4.Also, you can use the
+=operator to add to the current value of a variable, iecan be changed to:
EDIT2: One other thing. By convention, constant
#defines should be in upper-case, ie.#define HOUR 60as opposed to#define hour 60.