Hello everyone,
Well, it seems my struggles with structures have continued. Shortly after getting my code to work, I wanted to pass the entire structure into a function. Trouble is, I want to use a typedef as described below:
================================================ -
typedef struct
-
{
-
int depart [FLIGHTS];
-
int arrive[FLIGHTS];
-
const char *attendant;
-
} flight; /*flight is now a new structure type*/
-
===============================================
And here is how I wanted to call my function:
=============================================== -
#include <stdio.h>
-
-
int main(int argc, char *argv[])
-
{
-
-
int time, closest;
-
-
struct flight
-
{
-
int depart[FLIGHTS];
-
int arrive[FLIGHTS];
-
char *attendant;
-
};
-
-
struct flight schedule[] =
-
{{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"}};
-
-
closest = find_departure(time, schedule);
-
-
return 0;
-
}
-
===============================================
This time, I get an error that says "Undefined symbol find_departure", meaning there's something wrong with my find_departure function. Here is my function prototype:
=============================================== -
int find_departure(int time, flight schedule[]);
-
==============================================
Is there a fix to this? (By the way, 'FLIGHTS' is a macro defined in my header file standing for the integer value '8'. Also, I hope I don't get flamed for making a new thread, but I thought this topic was rather different than the issue I posted earlier this morning.)
Thanks.
RICH
12 4154
Where u have put the prototype of the function find_departure.
Is it before the main function ?
And where is the body of that function?
Raghu
How do you expect function find_departure to know how many elements are in the schedule array? You have to pass it the number of elements in the array.
C supports passing structures as arguments and having structure return values; but in my experience life is better if you instead pass pointers to structures back and forth between caller and callee. (That's actually pretty much what you're doing, but you're trying to hide it.)
@nexusdarkblue
No. It means exactly as it reads - compiler has no idea what "find_departure" is, whether it wrong or not: you declare you function somewhere else and don't include it into your source file called "And here is how I wanted to call my function:" as it includes only "stdio.h"
Okay, here is my latest version of my code with the following changes:
-I've decided to declare 'depart' and 'arrive' as simple integers because I realized I'm only going to be using one depart and one arrive value for each of my structure's members.
-I forgot I had written a "defs.h" file which includes my function prototype, macro definition, and the includes for the standard input/output and string functions. This header now appears at the top of my program.
-I've also included the body of my 'find_departure' function
So here is everything as it appears now with the above changes: main( ) - 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;
-
}
find_depart( ) body - /*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;
-
/*compares 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;
-
}
-
defs.h -
#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[]);
-
I hope this makes it more clear as to what I'm trying to accomplish here. Thanks to everyone's help and input thus far.....
RICH
How do you choose to denote midnight in the arrive or depart field? I suggest that "2400" is better than "0". An uninitialized flight structure (or one lacking an initializer value for arrive or depart) will assign the value "0". Life is easier for you if "0" is an obviously invalid value.
I suggest you provide a MIDNIGHT macro and then use that macro in your initializers instead of a literal number.
Are you sure you don't need arrival and departure dates too?
Hey donbock,
You know, I didn't even think about using '2400' instead of '0' to denote midnight. I think I will define a macro for that. I plan to include arrival and departure dates later on once I get everything else working. And then I just remembered that I need to test for invalid input as well. So thanks for that.
The main issue, though, lies in me trying to pass my structure to my function. When I try to compile it, I get the error message " syntax error before 'flight' ", and I can't figure out why. Is it because I have my structure defined as a typedef and structures can't be passed to functions this way?
@nexusdarkblue
The compiler error message ought to report a line number. Please relate that line number to the equivalent line in the source listings you provided (msg #5 or #1). Please provide the complete compiler error message (cut and paste).
Donbock,
From Message #5, the line that is causing the error from the source code I provided occurs in Line #19 of my main( ) function, which is the first block of code in Message 5. Here is the complete error message from my compiler: !gcc main.c && ./a.out
main.c: In function `main':
main.c:22: error: syntax error before "flight"
Hope this clarifies things a bit.
Standard C only permits you to define variables immediately after an open brace or immediately after another variable definition. You should move lines 6 and 7 below the structure initializer.
Don't include types in function's argument list when calling it:
find_departure(int time, schedule)
In function find_departure (and its prototype), you should avoid using the name "time" for the first argument. "time" is the name of a Standard C Library function in <time.h>. You can get away with using it as long as you don't include <time.h>, but it is bad form to overload standard names.
Try this as an example:
void displayOutput (struct mailBox *); //Prototype
............
typedef struct mailBox{
int number;
char state[TOTAL_NUMBER_OF_CHAR];
};
.........
struct mailBox mailBoxes[TOTAL_MAILBOXES]; //Declaration
displayOutput(mailBoxes);//Passing array of structs to a function
.........
void displayOutput(struct mailBox * mB){ //Function itself
}
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
19 posts
views
Thread by kelvSYC |
last post: by
|
8 posts
views
Thread by Vijay Kumar R Zanvar |
last post: by
|
6 posts
views
Thread by archilleswaterland |
last post: by
|
47 posts
views
Thread by Albert |
last post: by
|
12 posts
views
Thread by addinall |
last post: by
|
11 posts
views
Thread by skumar434 |
last post: by
|
12 posts
views
Thread by Fred |
last post: by
|
3 posts
views
Thread by Amit_Basnak |
last post: by
|
4 posts
views
Thread by uidzer0 |
last post: by
| | | | | | | | | | |