is there any way to pass a structure vairable to a procedure? i've tried but it doesnt seem to work. 44 errors =/
is there any way to pass a structure vairable to a procedure? i've tried but it doesnt seem to work. 44 errors =/
Do you mean the entire structure or just a variable in the structure? Either way, you should be able to do it. Here is an example of passing a structure:
Although if you want the function to modify its contents you should pass it as a pointer or a referenceCode:#include <iostream> struct MyStruct { int a; }; int myFunc(MyStruct obj) { std::cout<<obj.a<<std::endl; } int main() { MyStruct MyObj; MyObj.a = 10; myFunc(MyObj); }
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
i need the function to modify it, time to learn pointers. thanks.
Or you could just pass it by reference.
Using the above code:
Code:int myFunc(MyStruct &obj) { obj.a = 100; }
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
Shh...wait till he learns pointers, then tell him that
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
do i have to declare the structure at the top? cause that would screw up my program a bit
You have to declare it before you use it. You can put it in a header if you want and include the header at the top in any source files you wish to use the structure in.
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers