Structures and Functions

This is a discussion on Structures and Functions within the C++ Programming forums, part of the General Programming Boards category; Can anybody please give me an example of how to pass a structure via. functions. I hope it is possible, ...

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

    Structures and Functions

    Can anybody please give me an example of how to pass a structure via. functions. I hope it is possible, but I'm not sure. If at all possible, I'd like to pass it by value, not really by reference, but it wouldn't hurt to learn both ways.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    It's usually more efficient to pass a structure by reference, as it saves space on the system stack:

    Code:
    #include <iostream.h>
    
    typedef struct{
     int mymember;
    } MYSTRUCT;
    
    
    int SomeFunction(MYSTRUCT &mys){ // passing by reference
     mys.mymember=5;
     return 0;
    }
    
    int main(){
     MYSTRUCT struc;
     struc.mymember=2;
     cout << "mymember=" << struc.mymember << endl; // "mymember=2"
     SomeFunction(struc);
     cout << "mymember=" << struc.mymember << endl; // "mymember=5"
     return 0;
    }

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    You just pass it like any other data type:
    Code:
    struct myStruct
    {
       int x,y;
    }
    
    void myFunc1(myStruct m)    // by value
    {
        cout<<m.x<<" "<<m.y<<endl;
    }
    
    void myFunc2(myStruct &m)  // by reference
    {
        cout<<m.x<<" "<<m.y<<endl;
    }
    
    int main()
    {
        myStruct m;
        m.x=1;
        m.y=2;
        myFunc1(m);
        myFunc2(m);
        return 0;
    }

  4. #4
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Originally posted by poccil
    It's usually more efficient to pass a structure by reference, as it saves space on the system stack:

    Code:
    #include <iostream.h>
    
    typedef struct{
     int mymember;
    } MYSTRUCT;
    
    
    int SomeFunction(MYSTRUCT &mys){ // passing by reference
     mys.mymember=5;
     return 0;
    }
    
    int main(){
     MYSTRUCT struc;
     struc.mymember=2;
     cout << "mymember=" << struc.mymember << endl; // "mymember=2"
     SomeFunction(struc);
     cout << "mymember=" << struc.mymember << endl; // "mymember=5"
     return 0;
    }
    Adding to what he said, if you want to pass it to a function like regular but also save memory, pass it as a const reference.
    Code:
    struct  random{
    int i;
    int j;
    };
    
    int  function(const random &num);
    That will work like normal and save space.
    If you ever need a hug, just ask.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    Ok. Say I had two switch statements, both are quite long and drawl out, and the only difference is a Name in the structure. All i really want to do is make a function that will accept either of the two structures.
    like if i had a structure named MyStru1, and I also had MyStru2. I could pass either into the function. Possible?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. COntrol structures and functions questions
    By angelicscars in forum C Programming
    Replies: 1
    Last Post: 11-21-2005, 10:50 AM
  2. passing structures to functions
    By AmazingRando in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2003, 11:22 AM
  3. Array of Structures and Functions
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 05-04-2003, 07:06 AM
  4. passing array structures to functions
    By lukejack in forum C Programming
    Replies: 2
    Last Post: 04-08-2003, 02:17 PM
  5. data structures / hash functions
    By rickc77 in forum C Programming
    Replies: 5
    Last Post: 11-11-2001, 12:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21