Robolisk
Robolisk

Reputation: 1792

How to pass a struct array into a function

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;
}

Upvotes: 0

Views: 1088

Answers (2)

AusCBloke
AusCBloke

Reputation: 18492

Your distance function is all wrong. Since you have a struct that contains flight_time and flight_layover, you want to pass an array of these structs to the function, not an array of each of those int values. ie.

int distance(int start, int end, struct flight flights[])
{
    int total_mins=0;
    int i=0;
    for (i=end+1;i--;i=start)
    {
        total_mins=total_mins + flights[i].flight_time + flights[i].flight_layover;
    }

    return total_mins;
}

Changed the function signature and the line inside the for loop.

Then where you call distance, you can change the call to:

distance(user_start,user_end, flights)

That'll pass a pointer to the start of your array (flights), and user_start and user_end will specify the bounds of the array to be used to calculate the distance.

Also note that you could be accessing indexes out of the flights array's bounds. I especially don't understand why you have i=end+1, perhaps you wanted i = end - 1?

You also have the condition and decrement part of your for loop reversed, this is how I think it should be:

for (i = start; i <= end; i++)

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 getNum function, 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, ie

total_mins=total_mins + flights[i].flight_time + flights[i].flight_layover;

can be changed to:

total_mins += flights[i].flight_time + flights[i].flight_layover;

EDIT2: One other thing. By convention, constant #defines should be in upper-case, ie.

#define HOUR 60 as opposed to #define hour 60.

Upvotes: 2

K-ballo
K-ballo

Reputation: 81349

I believe you want something like this:

int distance(int start, int end , struct flight flights[5])
{
    int total_mins=0;
    int i=0;
    for (i=end+1;i--;i=start)
    {
        total_mins=total_mins + flights[i].flight_time + flights[i].flight_layover;
    }

    return total_mins;
}

which is then called as

travel_time = distance(user_start,user_end, flights);

Upvotes: 0

Related Questions