is there a way to set up a structure in my main and then pass the entire structure to my other functions with out passing each item individually? any help on pass structures or parts of structures is appeciated.
This is a discussion on passing a structure within the C++ Programming forums, part of the General Programming Boards category; is there a way to set up a structure in my main and then pass the entire structure to my ...
is there a way to set up a structure in my main and then pass the entire structure to my other functions with out passing each item individually? any help on pass structures or parts of structures is appeciated.
Yes... how you pass it depends on what you want to do with the struct within the function.
Code:struct foo { // Data members go here }; void func1(foo f) // Passed by copy { // Do stuff with f } void func2(foo* f) // Passed by pointer/reference { // Do stuff with f } void func3(foo& f) // Passed by reference { // Do stuff with f } int main() { foo bar; func1(bar); // Passed by copy func2(&bar); // Passed by pointer/reference func3(bar); // Passed by reference return 0; }
I used to be an adventurer like you... then I took an arrow to the knee.
why not use pointers? because every time you pass it, a copy is made.