Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6800983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:02:30+00:00 2026-05-26T19:02:30+00:00

I want to pass a struct array into a function and I keep getting

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:02:31+00:00Added an answer on May 26, 2026 at 7:02 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to pass an array of arbitrary struct pointers and a comparison function
I want to pass in the tType of a class to a function, and
I want to pass a struct within a method so i can change it's
I want to pass a pointer to pointer to a function, allocate memory in
I pass the custom data type of C# struct and now I want to
I want to pass an integer value to a form in .Net so that
I want to pass an int list (List) as a declarative property to a
I want to pass an enum value as command parameter in WPF, using something
I want to pass some parameters to my MVC UserControl like ShowTitle(bool) and the
I want to pass a variable to a UIButton action, for example NSString *string=@one;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.