is there any way to pass a structure vairable to a procedure? i've tried but it doesnt seem to work. 44 errors =/
Printable View
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);
}
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;
}
Shh...wait till he learns pointers, then tell him that ;)
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.