473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Structures As Function Arguments

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:
================================================
Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. {
  3.         int depart [FLIGHTS];
  4.         int arrive[FLIGHTS];
  5.         const char *attendant;
  6. } flight;  /*flight is now a new structure type*/
  7.  
===============================================

And here is how I wanted to call my function:

===============================================
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main(int argc, char *argv[])
  4. {
  5.  
  6. int time, closest;
  7.  
  8. struct flight
  9. {
  10. int depart[FLIGHTS];
  11. int arrive[FLIGHTS];
  12. char *attendant;
  13. };
  14.  
  15. struct flight schedule[] =
  16. {{800}, {1016}, "Jason Mackenzie"},
  17. {{943}, {1152}, "Valerie Woods"},
  18. {{1119}, {1331}, "Antonio Vasquez"},
  19. {{1247}, {1500}, "Natalie McIver"}, 
  20. {{1400}, {1608}, "Scott Curtis"},
  21. {{1545}, {1755}, "Yvonne Vogelar"},
  22. {{1900}, {2120}, "Mitch Matthews"},
  23. {{2145}, {2358}, "Marcie Maddox"}};
  24.  
  25. closest = find_departure(time, schedule);
  26.  
  27. return 0;
  28. }
  29.  
===============================================

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:

===============================================
Expand|Select|Wrap|Line Numbers
  1. int find_departure(int time, flight schedule[]);  
  2.  
==============================================

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
Mar 8 '09 #1
12 4246
gpraghuram
1,275 Expert 1GB
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
Mar 9 '09 #2
donbock
2,426 Expert 2GB
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.)
Mar 9 '09 #3
newb16
687 512MB
@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"
Mar 9 '09 #4
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( )

Expand|Select|Wrap|Line Numbers
  1.  int main(int argc, char *argv[])
  2. {
  3.  
  4.         int time, closest;
  5.  
  6.         puts("Enter a time in military hours and minutes:");
  7.         scanf("%d", &time);
  8.  
  9.         flight schedule[] =   /*flight is a structure type*/
  10.         {{800, 1016, "Jason Mackenzie"},
  11.           {943, 1152, "Valerie Woods"},
  12.           {1119, 1331, "Antonio Vasquez"},
  13.           {1247, 1500, "Natalie McIver"},
  14.           {1400, 1608, "Scott Curtis"},
  15.           {1545, 1755, "Yvonne Vogelar"},
  16.           {1900, 2120, "Mitch Matthews"},
  17.           {2145, 2358, "Marcie Maddox"}};
  18.  
  19.         find_departure(time, flight schedule[]);
  20.  
  21.         return 0;
find_depart( ) body

Expand|Select|Wrap|Line Numbers
  1.  /*function to search flight schedule for times closest to time put in by user*
  2. /
  3.  
  4. #include "defs.h"
  5.  
  6. int find_departure(int time, flight schedule[])
  7. {
  8.         int j = 0; /*counter to search schedule*/
  9.  
  10.         int closest = time - schedule.depart[0];
  11.         /*initially, the closest flight time is the first departure*/
  12.  
  13.         int temp;
  14.         /*compares each departure time with current closest flight time*/
  15.  
  16.         if (closest < 0)
  17.         {
  18.                 closest *= -1;  /*set closest as absolute value if negative*/
  19.         }
  20.  
  21.         while (j <= FLIGHTS)
  22.         {
  23.                 temp = time - schedule.depart[j];
  24.  
  25.                 if (temp < 0)
  26.                 {
  27.                         temp *= -1;  /*temp is absolute value if negative*/   
  28.  
  29.                 }
  30.  
  31.                 if (temp < closest)
  32.                 {
  33.                         closest = schedule.depart[j];
  34.                         /*current departure is now closest to user's input*/
  35.                 }
  36.  
  37.                 j++; /*check next departure time*/
  38.         }
  39.  
  40.         return closest;
  41. }
  42.  
defs.h

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define FLIGHTS 8
  5.  
  6. typedef struct
  7. {
  8.         int depart;
  9.         int arrive;
  10.         const char *attendant;
  11. } flight;  /*flight is now a new structure type*/
  12.  
  13. int find_departure(int time, flight schedule[]);
  14.  
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
Mar 9 '09 #5
donbock
2,426 Expert 2GB
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?
Mar 9 '09 #6
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?
Mar 9 '09 #7
donbock
2,426 Expert 2GB
@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).
Mar 10 '09 #8
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.
Mar 10 '09 #9
donbock
2,426 Expert 2GB
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.
Mar 10 '09 #10
newb16
687 512MB
Don't include types in function's argument list when calling it:

find_departure(int time, schedule)
Mar 10 '09 #11
donbock
2,426 Expert 2GB
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.
Mar 10 '09 #12
Jibran
30
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
}
Mar 11 '09 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

19
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...
8
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 ...
6
by: archilleswaterland | last post by:
structures typedef struct{ char name; int age; float balance; }account; account xyx; accout *ptr;
47
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...
12
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...
11
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;
12
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...
3
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...
4
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
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...
0
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...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.