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.
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; }
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
why not use pointers? because every time you pass it, a copy is made.