StructToCharArray

Copia a estrutura POD para a matriz do tipo uchar.

bool  StructToCharArray(
   const void&  struct_object,     // estrutura
   uchar&       char_array[],      // matriz
   uint         start_pos=0        // posição inicial na matriz
   );

Parâmetros

struct_object

[in]  Referência para qualquer tipo de estrutura POD (estruturas contendo apenas tipos de dados simples).

char_array[]

[in]  Matriz do tipo uchar.

start_pos=0

[in]  Posição na matriz a partir da qual serão copiados os dados.

Valor retornado

Retorna true em caso de sucesso, caso contrário, false.

Observação

Ao copiar, a matriz dinâmica é expandida automaticamente (ArrayResize) se não houver espaço suficiente. Se não for possível expandir a matriz para o valor requerido, a função retornará um erro.

 

Exemplo:

uchar    ExtCharArray[];
MqlRates ExtRates[1];
 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- copiamos na estrutura MqlRates os dados de uma barra atual
   if(CopyRates(Symbol(), PERIOD_CURRENT01ExtRates)!=1)
     {
      Print("CopyRates() failed. Error code: "GetLastError());
      return;
     }
 
//--- imprimimos no log os dados recebidos na estrutura MqlRates
   Print("Data in the structure immediately after receiving it:");
   MqlRatesPrint(ExtRates[0]);
 
//--- copiamos a estrutura da página em um array do tipo uchar
   ResetLastError();
   if(!StructToCharArray(ExtRates[0], ExtCharArray))
     {
      Print("StructToCharArray() failed. Error code: "GetLastError());
      return;
     }
 
//--- limpamos a estrutura
   ZeroMemory(ExtRates[0]);
//--- imprimimos os dados na estrutura após a limpeza
   Print("\nData in the structure after ZeroMemory():");
   MqlRatesPrint(ExtRates[0]);
 
//--- agora vamos copiar os dados do array uchar para a estrutura MqlRates
   if(!CharArrayToStruct(ExtRates[0], ExtCharArray))
     {
      Print("CharArrayToStruct() failed. Error code: "GetLastError());
      return;
     }
//--- imprimimos os dados na estrutura MqlRates depois de copiá-los do array uchar
   Print("\nData in the MqlRates structure after copying it from a uchar array:");
   MqlRatesPrint(ExtRates[0]);
 
   /*
  resultado:
 
   Data in the structure immediately after receiving it:
   MqlRates data:
    Time = 2024.02.21 07:00;
    Open = 1.08143;
    High = 1.08158;
    Low = 1.08122;
    Close = 1.08137;
    Tick volume = 1341;
    Spread = 4;
    Real volume = 0
 
   Data in the structure after ZeroMemory():
   MqlRates data:
    Time = 0;
    Open = 0.00000;
    High = 0.00000;
    Low = 0.00000;
    Close = 0.00000;
    Tick volume = 0;
    Spread = 0;
    Real volume = 0
 
   Data in the MqlRates structure after copying it from a uchar array:
   MqlRates data:
    Time = 2024.02.21 07:00;
    Open = 1.08143;
    High = 1.08158;
    Low = 1.08122;
    Close = 1.08137;
    Tick volume = 1341;
    Spread = 4;
    Real volume = 0
   */
  }
//+------------------------------------------------------------------+
//| Imprimimos no log os campos da estrutura MqlRates                |
//+------------------------------------------------------------------+
void MqlRatesPrint(MqlRates &rates)
  {
//--- criamos uma string de saída
   string text=StringFormat(" Time = %s;\n"
                            " Open = %.*f;\n"
                            " High = %.*f;\n"
                            " Low = %.*f;\n"
                            " Close = %.*f;\n"
                            " Tick volume = %I64u;\n"
                            " Spread = %d;\n"
                            " Real volume = %I64u",
                            TimeToString(rates.time),
                            _Digitsrates.open,
                            _Digitsrates.high,
                            _Digitsrates.low,
                            _Digitsrates.close,
                            rates.tick_volume,
                            rates.spread,
                            rates.real_volume);
//--- imprimimos no log o título e a string de saída
   Print("MqlRates data:\n"text);
  }

Veja também

StringToCharArray, ShortArrayToString,CharArrayToStruct, Uso da página de código, FileWriteStruct, União (union), MathSwap