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 4246
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
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: kelvSYC |
last post by:
Can variable functions be implemented in C, and if so, how?
Also, can you have a function or a function prototype as a member in a
structure?
--
I am only a mirage.
--
comp.lang.c.moderated...
|
by: Vijay Kumar R Zanvar |
last post by:
Hi c.l.c,
The following table show the general structure
of a header:
Byte # Field Description
------ -----------------
0 Extended message flag (01H)
1 ...
|
by: archilleswaterland |
last post by:
structures
typedef struct{
char name;
int age;
float balance;
}account;
account xyx;
accout *ptr;
|
by: Albert |
last post by:
So structures are useful to group variables, so you can to refer to a
collection as a single entity. Wouldn't it be useful to also have the
ability to collect variable and functions?
Ask K&R...
|
by: addinall |
last post by:
Hi guys and gals,
Haven't been in here for a good decade or so!
I'm having a braid-dead moment that is lasting for
a couple of days. Declaring pointers to functions
witrhin structures and...
|
by: skumar434 |
last post by:
Hi everybody,
I am faceing problem while assigning the memory dynamically to a array
of structures .
Suppose I have a structure
typedef struct hom_id{
int32_t nod_de;
int32_t hom_id;
|
by: Fred |
last post by:
Is it possible to create a function which will take any
number of structures as an argument? For example, what
if I wanted depending on the circumstances to pass a
structure named astruct, or...
|
by: Amit_Basnak |
last post by:
Dear Friends
I have the follwoing function "tss_fe_get_own_info" which has the
arguments as shows below
tss_fe_get_own_info(char *user_id, tss_user_profile_t
**p_buf_UserSecInfo, error_status_t...
|
by: uidzer0 |
last post by:
Hey everyone,
I apologize in advance for this novice question however I'm not having
any luck finding the answer myself.
I'm attempting to loop through an array of structures passed to a...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |