469,358 Members | 1,605 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

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 4046
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,422 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,422 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,422 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,422 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,422 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

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
12 posts views Thread by addinall | last post: by
4 posts views Thread by uidzer0 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.