StructToCharArray

Copia una estructura POD a una matriz de tipo uchar.

bool  StructToCharArray(
   const void&  struct_object,     // estructura
   uchar&       char_array[],      // matriz
   uint         start_pos=0        // posición inicial en la matriz
   );

Parámetros

struct_object

[in]  Enlace a cualquier tipo de estructura POD  (estructura que contiene solo tipos simples de datos).

char_array[]

[in]  Matriz del tipo uchar.

start_pos=0

[in]  Posición en la matriz, a partir de la cual se copiarán los datos.

Valor retornado

Retorna true en el caso de éxito, de lo contrario, false.

Observación

Durante el copiado, la matriz dinámica se expande automáticamente (ArrayResize), si en ella no hay espacio suficiente. Si no se ha logrado expandir la matriz hasta el tamaño necesario, la función retornará error.

 

Ejemplo:

uchar    ExtCharArray[];
MqlRates ExtRates[1];
 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- copiamos los datos de la barra actual en la estructura MqlRates
   if(CopyRates(Symbol(), PERIOD_CURRENT01ExtRates)!=1)
     {
      Print("CopyRates() failed. Error code: "GetLastError());
      return;
     }
 
//--- imprimimos en el registro los datos recibidos en la estructura MqlRates
   Print("Data in the structure immediately after receiving it:");
   MqlRatesPrint(ExtRates[0]);
 
//--- copiamos la estructura en una array del tipo uchar
   ResetLastError();
   if(!StructToCharArray(ExtRates[0], ExtCharArray))
     {
      Print("StructToCharArray() failed. Error code: "GetLastError());
      return;
     }
 
//--- limpiamos la estructura
   ZeroMemory(ExtRates[0]);
//--- imprimimos los datos en la estructura después de realizar la limpieza
   Print("\nData in the structure after ZeroMemory():");
   MqlRatesPrint(ExtRates[0]);
 
//--- ahora compiamos los datos del array uchar a la estructura MqlRates
   if(!CharArrayToStruct(ExtRates[0], ExtCharArray))
     {
      Print("CharArrayToStruct() failed. Error code: "GetLastError());
      return;
     }
//--- imprimimos los datos en la estructura MqlRates después de copiar del 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
   */
  }
//+------------------------------------------------------------------+
//| Imprime en el registro los campos de la estructura MqlRates      |
//+------------------------------------------------------------------+
void MqlRatesPrint(MqlRates &rates)
  {
//--- creamos una línea de muestra
   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 en el registro de encabezados y la línea de muestra
   Print("MqlRates data:\n"text);
  }

Ver también

StringToCharArray, ShortArrayToString,CharArrayToStruct, Uso de la página de código, FileWriteStruct, Unión (union), MathSwap