Passing An Array Of Structures To A Function

This is a discussion on Passing An Array Of Structures To A Function within the C Programming forums, part of the General Programming Boards category; Hi everyone, It seems my struggles with structures have continued. In my current program, I'm trying to pass an array ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    Passing An Array Of Structures To A Function

    Hi everyone,

    It seems my struggles with structures have continued. In my current program, I'm trying to pass an array of structures into a function. I've declared my structure in a separate header file using typedef to define a new type called 'flight'. But when I try to compile my program, I get the following error message:

    "Syntax error before flight"

    Here is my code for main(), my header file "defs.h", and my function "find_depart()", respectively:

    ------------------------------------------------------------------------------------------------------
    Code:
    #include "defs.h"
    
    int main(int argc, char *argv[])
    {
    
            int time, closest;
    
            puts("Enter a time in military hours and minutes:");
            scanf("%d", &time);
    
            flight schedule[] =   /*flight is a structure type*/
            {{800, 1016, "Jason Mackenzie"},
              {943, 1152, "Valerie Woods"},
              {1119, 1331, "Antonio Vasquez"},
              {1247, 1500, "Natalie McIver"},
              {1400, 1608, "Scott Curtis"},
              {1545, 1755, "Yvonne Vogelar"},
              {1900, 2120, "Mitch Matthews"},
              {2145, 2358, "Marcie Maddox"}};
    
            find_departure(time, flight schedule[]);
    
            return 0;
    }
    ------------------------------------------------------------------------------------------------------
    Code:
    #include <stdio.h> 
    #include <string.h> 
      
    #define FLIGHTS 8 
      
    typedef struct 
    { 
            int depart; 
            int arrive; 
            const char *attendant; 
    } flight;  /*flight is now a new structure type*/ 
      
    int find_departure(int time, flight schedule[]);
    ------------------------------------------------------------------------------------------------------
    Code:
    /*function to search flight schedule for times closest to time put in by user*/
    
    #include "defs.h"
    
    int find_departure(int time, flight schedule[])
    {
            int j = 0; /*counter to search schedule*/
    
            int closest = time - schedule.depart[0];
            /*initially, the closest flight time is the first departure*/
    
            int temp;
            /*used to compare each departure time with current closest flight time*/
    
            if (closest < 0)
            {
                    closest *= -1;  /*set closest as absolute value if negative*/
            }
    
            while (j <= FLIGHTS)
            {
                    temp = time - schedule.depart[j];
    
                    if (temp < 0)
                    {
                            temp *= -1;  /*temp is absolute value if negative*/
                    }
    
                    if (temp < closest)
                    {
                            closest = schedule.depart[j];
                            /*current departure is now closest to user's input*/
                    }
    
                    j++; /*check next departure time*/
            }
    
            return closest;
    }
    If someone could please tell me what I could be doing wrong, I would greatly appreciate it--thanks!

    RICH

  2. #2
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,754
    schedule.depart[0]

    should be

    schedule[0].depart
    To be or not to be == true

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    Vart,

    I made the change to my array subscripting and membership selection, but I'm still getting errors. I want to pass my array of structures into the function called in main ( ). Thanks.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,807
    Then just pass it:
    Code:
    find_departure(time, flight schedule);
    Note that the array will be converted to a pointer to its first element when it is passed to find_departure().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-31-2009, 02:44 PM
  2. passing an array to function
    By waysgoose in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2008, 03:59 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21