Rafael Caetano Pinto: When I update the values by reference, the struct is updated, but the struct in the array is not.
You have two variables. You only update ms. Of course, arr[0] is not.
William Roeder:
You have two variables. You only update ms. Of course, arr[0] is not.
You answer in same time that I was updating the question with more info :)
Yes, I thougth that when I set a variable I'm creating a object copy and not a simple memory reference.
There is some way to create a variable by reference to a struct?
MyStruct& myRef = arr[0];
I have some loops and to works, I had to change the code from this:
for(int i=ArraySize(stocks)-1; i>=0; i--){ StockSymbol ss = stocks[i]; if(ss.enabled){ double bid = SymbolInfoDouble(ss.symbol, SYMBOL_BID); double ask = SymbolInfoDouble(ss.symbol, SYMBOL_ASK); if(ss.bid != bid || ss.ask != ask){ if(StockSymbol::Update(ss)){ OnMultiCurrencyTick(ss); ss.bid = bid; ss.ask = ask; }; } } else { StockSymbol::Update(ss); } }to this:
for(int i=ArraySize(stocks)-1; i>=0; i--){ if(stocks[i].enabled){ double bid = SymbolInfoDouble(stocks[i].symbol, SYMBOL_BID); double ask = SymbolInfoDouble(stocks[i].symbol, SYMBOL_ASK); if(stocks[i].bid != bid || stocks[i].ask != ask){ if(StockSymbol::Update(stocks[i])){ OnMultiCurrencyTick(stocks[i]); stocks[i].bid = bid; stocks[i].ask = ask; }; } } else { StockSymbol::Update(stocks[i]); } }
The readability of the code has gotten worse, but I don't seem to have much of a choice here.
Rafael Caetano Pinto:
I have some loops and to works, I had to change the code from this:
to this:
Turn the struct into a class and with pointers you can achieve your goal.
I have some loops and to works, I had to change the code from this:
to this:
The readability of the code has gotten worse, but I don't seem to have much of a choice here.
Laszlo Tormasi:
Turn the struct into a class and with pointers you can achieve your goal.
Turn the struct into a class and with pointers you can achieve your goal.
Tks, this approach resolved the problem in a more elegant way.

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
I have a array of structs and need to update some values there. When I update the values by reference, the struct is updated, but the struct in the array is not. I know that is a C behavior, but I was unable to find a good way to do this update. Do you have some way to properly update structs in an array?
In the code below, I tested 2 cases:
Case 1: I create a variable with the arr[0] and passed this variable to the function. Only the variable changes.
Case 2: I create a variable with the arr[0] but passed the arr[0] to the function. Only the arr[0] changes.
This make me think that the variables are copies of the contents and not references... but I cant create a reference variable to a struct (MyStruct& is not allowed by the compilator).
The only that I though was manually set the array position every time that I change some struct value... This is the right approach?
Tks by advance.
Code:
Log: