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:

--------------------------------------------------------------------------------------

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

--------------------------------------------------------------------------------------

#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[]);

--------------------------------------------------------------------------------------

/*function searches schedule for times closest to input 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 departure time with current closest time*/

        if (closest < 0)
        {
                closest *= -1;  /*set 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

Recommended Answers

All 4 Replies

Try moving your definition of flight schedule[] to the beginning of main.

Line #21 in main module: why type specifier in a function call?..

find_departure(time,shedule);

It's the same error as

void f(int a, int b);
...
f(1, int 2);

Congratulations, it's not a specific struct issue; stop your struggle, better read textbooks ;)...

Line #21 in main module: why type specifier in a function call?..

find_departure(time,shedule);

It's the same error as

void f(int a, int b);
...
f(1, int 2);

Congratulations, it's not a specific struct issue; stop your struggle, better read textbooks ;)...

Hi, and thanks for responding.

Yes, I tried typing in the statement find_departure(time,schedule); exactly as you have mentioned. However, upon doing so, I get the following error messages:

!gcc main.c && ./a.out
Undefined first referenced
symbol in file
find_departure /var/tmp//cciG6443.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

From my experience, passing an array should be as simple as using only the name, which is why your suggestion should be correct.

My function is defined in my "defs.h" file, so it shouldn't be as if the compiler doesn't recognize it. What's wrong here?

You're not linking with the other code file!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.