Thread: Passing Structures ??

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    Passing Structures ??

    Okay, In main I have the following declaration for structure

    Code:
    main()  {
    struct temp {
      char strg1[N],strg2[N];
      int test[N];
      float fraction;
    };
    
    struct temp S1[100];
    FILE *fp;
    
    
    somefunction(?????????);
    Okay, I am just looking for an example here. I would like to first, know what the prototype would be for somefunction(), how I would send both the structure S1 to the function somefunction and how to send the file pointer fp to the function somefunction() and finally, what somefunction would look like syntactically. Eg. somefunction(*fp,S1,etc...????)

    Thanks.

    I currently have them as globals and I was told that this was a not a good idea... so I am going to give it a try the other way. Thanks ahead of time again for any assistance.

  2. #2
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    Heres how I do it.

    Code:
    struct temp {
      char strg1[ N ],strg2[ N ];
      int test[ N ];
      float fraction;
    };
    
    somefunction( struct temp theStruct[ ] );
    
    main( void ) {
      struct temp S1[ 100 ];
      FILE *fp;
    
      somefunction( S1 );
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    RE

    so you put the structure itself outside of the main() ?? and what about the file pointer?

  4. #4
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    You put the structure definition outside the main so that other functions can define a structure ( like defining a char or int ). The file pointer is just fine inside main.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    RE

    Okay, so the structure is global... okay I get that, how do i send the file pointer to functions other than main() ?? also, in the function, how do i redeclare the structure?


    somefunction(s2) {

    }

    would that work ?? or do i need more?
    Last edited by justin69enoch; 02-11-2003 at 06:22 PM.

  6. #6
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    Since the type FILE is defined in c lib:

    Code:
    struct temp {
      char strg1[ N ],strg2[ N ];
      int test[ N ];
      float fraction;
    };
    
      /* prototype: you have declared a structure 
      ** that will "carry" what you pass */
    somefunction( struct temp theStruct[ ], FILE *theFilePointer );
    
    main( void ) {
      struct temp S1[ 100 ];
      FILE *fp;
    
      somefunction( S1, fp );
    
      return 0;
    }
    
    somefunction( struct temp theStruct[ ], FILE *theFilePointer ) {
      /* because the struct definition is global you 
      ** can declare another structure as well */
      struct temp anotherStruct;
      /* but this is a completely seperate new struct, just the same type */
    }
    Last edited by dharh; 02-11-2003 at 06:29 PM.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    last question.. I promise..

    Thanks for all of you help, I have learned a lot in the last hour or so. Okay so now I understand the prototype and and function call from main(),

    In the function, how would i declare the variables? Like this:??


    Code:
    somefunction(struct temp S1,FILE *fp)  {
    
    function stuff ...
    
    }

  8. #8
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    Just as you declared the FILE *fp; in main you can also declare variables in functions. Like:

    Code:
    someFunction( ) {
      char myChar;
      int myInt;
      /* doing other function stuff */
    }
    However variables declared in a function, even in main are not accessible by other functions. The way to create access points is by passing parameters or returning values.

    Returning a value:


    Code:
    int someFunction( ) {
      char myChar;
      int myInt;
      /* doing other function stuff */
    
      return 1; 
    }
    When this function finishes it will return the value 1 to whatever called it. You can return pretty much anything, but only one thing at a time. To utilize a return is as simple as this:

    int myInt;

    myInt = someFunction( );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. passing structures to functions
    By AmazingRando in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2003, 11:22 AM
  4. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 07:15 AM
  5. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM