Update a struct in an array of structs

 

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:

struct MyStruct{ string value; };

void calc(MyStruct &s){
   s.value = "updated";
}

int OnInit(){
   MyStruct arr[1];
   MyStruct ms = {0};
   ms.value = "initialized";
   
   arr[0] = ms;
   Print(ms.value);
   Print(arr[0].value);

   //test1
   calc(ms);
   Print(ms.value);
   Print(arr[0].value);

   //test2
   MyStruct ms2 = arr[0];
   calc(arr[0]);
   Print(ms2.value);
   Print(arr[0].value);
} 

Log:

2021.05.04 15:21:12.189	2021.02.10 00:00:00   initialized
2021.05.04 15:21:12.189	2021.02.10 00:00:00   initialized
2021.05.04 15:21:12.189	2021.02.10 00:00:00   updated
2021.05.04 15:21:12.189	2021.02.10 00:00:00   initialized
2021.05.04 15:21:12.189	2021.02.10 00:00:00   initialized
2021.05.04 15:21:12.189	2021.02.10 00:00:00   updated

 
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?

something like this:

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:


The readability of the code has gotten worse, but I don't seem to have much of a choice here.

Turn the struct into a class and with pointers you can achieve your goal.
 
Laszlo Tormasi:
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.

Reason: