How to pass structure values between functions
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: How to pass structure values between functions

Hybrid View

  1. #1
    Join Date
    Dec 2012
    Posts
    1

    How to pass structure values between functions

    Code:
    struct NewPlayer
    {
    int level;
    int intelligence;
    int damage;
    
    };
    
    int CharacterInfo(NewPlayer MageWizard, int Clevel,int Cint, int Cdam)
    {
    cin >> Clevel;
    cin >> Cint;
    cin >> Cdam;
    }
    
    //*****************************************************************
    I need a seperate function here that will inherit the values from the 
    structure in the function above.My problem is I dont know how to create 
    a seperate function that will grab the values of the structure
    from CharacterInfo(NewPlayer MageWizard, int Clevel,int Cint, int Cdam)
    to later be called upon in int main()
    
    ***The purpose of the seperate function is to use the structure values
    from the function above to forumlate an equation that will return a single
    integer value that will later be displayed in int main()
    //******************************************************************
    
    int main()
    {
    NewPlayer Wizard;
    int Flevel, Fdamage, Fintelligence;
    
    CharacterInfo(Wizard, Flevel, Fdamage, Fintelligence)
    
    //*********************************************************
    use the new function here to display the returned int value
    //*********************************************************
    }

  2. #2
    Join Date
    Aug 2009
    Location
    Romania->Felnac
    Posts
    48

    Re: How to pass structure values between functions

    I updated the function definition and return value (this is because i didn't understood what are you going to return) and you were using cin in the function to read values into some parameters that were passed by value, anyway i hope i can explain some things about parameter passing:

    1) you don't pass those extra values if you want to use the values that are contained into your structure (if you pass the structure as parameter is enough)
    2) you have to decide if you pass "by value" of "by reference" (you can also learn to pass pointers instead of references)
    2.1) pass by value will create a copy of your structure and use that copy in the function (you might want to keep in mind that not all the types are copyable - after you understand the basics you can investigate cases where you need copy constructor and operator= for your structs/classes)
    Code:
    //pass struct by value
    //... your struct declaration...
    
    //this function will copy the struct you pass from main function (or anywhere else you call this function)
    void CharacterInfo(NewPlayer MageWizard) 
    {
    //use the values that are passed with NewPlayer structure - not other parameters
    	cout << "Level: " << MageWizard.level << '\n'; 
    	cout << "intelligence: " << MageWizard.intelligence << '\n';
    	cout << "damage: " << MageWizard.damage << '\n';
    }
    
    int main()
    {
    	NewPlayer Wizard;
    	
    	Wizard.level = 10;
    	Wizard.damage = 100;
    	Wizard.intelligence = 1000;
    	
    	CharacterInfo(Wizard);
    }
    2.2) pass by reference will use a reference to the same structure that is passed as parameter, this is more efficient if the structure is big and is useful if the function needs to update some state inside the same object that was passed as parameter.
    Code:
    //... your struct declaration...
    void CharacterInfo(NewPlayer& MageWizard) //notice the &
    {
    	cout << "Level: " << MageWizard.level << '\n';
    	cout << "intelligence: " << MageWizard.intelligence << '\n';
    	cout << "damage: " << MageWizard.damage << '\n';
            MageWizard.level = 20; //you can test what happens if you delete the & in the function definition
    }
    
    int main()
    {
    	NewPlayer Wizard;
    	
    	Wizard.level = 10;
    	Wizard.damage = 100;
    	Wizard.intelligence = 1000;
    	
    	CharacterInfo(Wizard);
    //level will be 20 if you pass by reference and unchanged if you pass by value (because the function will update a copy of what you passed)
    	cout << "Level: " << Wizard.level << '\n'; 
    	cout << "intelligence: " << Wizard.intelligence << '\n';
    	cout << "damage: " << Wizard.damage << '\n';
    }
    Last edited by Zlatomir; December 30th, 2012 at 08:44 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center