-
Passing Parameters
Ok, this is kinda confusing me, and what i thought should work, didn't. Lets say i have the following code:
Code:
int variable;
void function1(int *variable)
{
// do something
function2(int *variable);
}
void function2(int variable)
{
}
Of course, this wouldnt work because your passing a pointer to a non-pointer parameter. And you cant call
Code:
function2(int variable);
as that is not passing in the parameter correctly which was recieved in function 1.
I thought that i could get it to work by using:
Code:
function2(int &(*variable));
That way, it is passing the value of the pointer, hence what im after. But this doesnt work :(
So how can i call function2 inside function1? Do i need to change the structure of function2 to pass a pointer to it? If so, then how would i call it in function 1?
-
choices
You can;
Pass to function 2 by pointer;
Code:
#include<iostream>
void function2(int*);//forward declaration
void function1(int *variable)
{
// do something
function2(variable);
}
void function2(int *variable)
{
std::cout << *variable << std::endl;
}
int main()
{
int variable = 10;
function1(&variable);
return 0;
}
or by value
Code:
#include<iostream>
void function2(int);//forward declaration
void function1(int *variable)
{
// do something
function2(*variable);
}
void function2(int variable)
{
std::cout << variable << std::endl;
}
int main()
{
int variable = 10;
function1(&variable);
return 0;
}
You can also bring references into it....
Code:
#include<iostream>
void function2(int&);//forward declaration
void function1(int& variable)
{
// do something
function2(variable);
}
void function2(int& variable)
{
std::cout << variable << std::endl;
}
int main()
{
int variable = 10;
function1(variable);
return 0;
}
choices..choices....
-
Ah, well thanks. I like choices :) That helps a lot, though i actually found the problem in my code to be:
which should be But now i know how to pass the variables correctly, i found it :) Thanks.