Reputation: 3
I am using a structure such as
struct IF_ID {
int PC;
string instruction;
};
and then in the
int main()
{
IF_ID stage1;
stage1.PC=0;
FETCH(stage1);
DECODE(&stage1);
return 0;
}
When I passe stage1
in the FETCH(stage1)
function it works fine, the thing is that i need the values that has been calculated in the FETCH(stage1)
to be used again in the second function DECODE(stage1)
so if the stage1.PC
is equal to 5 for example i need to reuse it in the DECODE(stage1)
function how can i do that??
Upvotes: 0
Views: 95
Reputation: 1381
Yochai Timmer and several others' answers are correct, but also consider making your struct into a class, and making these functions members of the class, since they are primarily operating on it. Then the code would look something like:
int main()
{
IF_ID stage1;
stage1.PC=0;
stage1.FETCH();
stage1.DECODE();
}
However, if FETCH uses none of the values of IF_ID except PC, and depending how your code is organised, it may be even clearer to make FETCH construct the object, rather than having a "blank" object passed in. For instance, something like:
struct IF_ID { int PC; string instruction; IF_ID() : PC(0) {} // Ensure default has PC=0 };
IF_ID Fetch() { IF_ID stage1; // do fetch stuff and put it in stage1 return stage1; // If IF_ID is a large class expensive to copy, // make sure your compiler supports "named return // value optimisation", ie. doesn't produce a // superfluous copy between the member variable // and the return value. }
int main() { IF_ID stage1 = Fetch(); Decode(stage1); }
Upvotes: 0
Reputation: 5102
Not completely sure what you're trying to do, but let me guess.
You have a type and two functions (FETCH and DECODE) that operate on an input parameter of that type. The first function modifies the input parameter and you want the modified values to be used as input to the second function.
Probably you should declare the functions as (let's call your type T, since this probably apply to any complex type or class).
void FETCH(T &x);
This way the parameter x of type T is passed by reference. The modifications performed within the body of the function will affect the result outside of the function.
Then you can declare the second function as:
void DECODE(const T &y);
Here you're again passing the complex type by reference, but this time you promise that you're not going to modify it (const).
Upvotes: 0
Reputation: 35059
The solution is to pass stage1
as a reference to the FETCH()
function. The function signature should look either like this:
void FETCH(IF_ID& stage);
or like this:
void FETCH(IF_ID* stage);
in the latter case, the function call also needs an ampersand sign in front of the parameter:
FETCH(&stage1);
Upvotes: 0
Reputation: 49251
You're passing the struct to FETCH() by value.
This means it is COPIED to the function.
If you want the actual struct to be passed, you need the method to receive its pointer or reference:
By reference:
void FETCH(IF_ID& ref_stage);
pointer to struct:
void FETCH(IF_ID* ref_stage);
If you use by reference, the inner function semantics wont change.
Upvotes: 3
Reputation: 37364
If you want FETCH
to change the value of parameter, it should accept either reference or pointer to your structure as parameter. Or it can return an instance IF_ID
.
Upvotes: 0