below is a sample on how to iterate over a variant array inside a variant. The sample is not complete and some checks (null pointer etc.) have been omitted for better readability.
OpcUa_Variant *pValue = &a_pMonitoredItems[i].Value.Value;
// check if value contains a variant array
if (pValue->Datatype == OpcUaType_Variant && pValue->ArrayType == OpcUa_VariantArrayType_Array)
{
// iterate over array elements
for (OpcUa_Int32 i = 0; i < pValue->Value.Array.Length; i++)
{
OpcUa_Variant *pArrayElement = &pValue->Value.Array.Value.VariantArray[i];
// the variant can contain anything, so we need to handle all combinations of array and data types:
switch (pArrayElement->ArrayType)
{
case OpcUa_VariantArrayType_Scalar:
{
switch (pArrayElement->Datatype)
{
case OpcUaType_Boolean:
// pArrayElement->Value.Boolean is set and can be used here
break;
case OpcUaType_SByte:
// pArrayElement->Value.SByte is set and can be used here
break;
// all data types have to be handled here ...
}
break;
}
case OpcUa_VariantArrayType_Array:
{
switch (pArrayElement->Datatype)
{
case OpcUaType_Boolean:
for (OpcUa_Int32 j = 0; j < pArrayElement->Value.Array.Length; j++)
{
// pArrayElement->Value.Array.Value.BooleanArray[j] is set and can be used here
}
break;
case OpcUaType_SByte:
for (OpcUa_Int32 j = 0; j < pArrayElement->Value.Array.Length; j++)
{
// pArrayElement->Value.Array.Value.SByteArray[j] is set and can be used here
}
break;
// all data types have to be handled here ...
}
break;
}
case OpcUa_VariantArrayType_Matrix:
{
// for matrix values, pArrayElement->Value.Matrix.Value contains n elements, where n is
// all dimensions multiplicated with each other:
OpcUa_Int32 numElements = 1;
for (OpcUa_Int32 dim = 0; dim < pArrayElement->Value.Matrix.NoOfDimensions; dim++)
{
numElements = numElements * pArrayElement->Value.Matrix.Dimensions[dim];
}
switch (pArrayElement->Datatype)
{
case OpcUaType_Boolean:
for (OpcUa_Int32 j = 0; j < numElements; j++)
{
// pArrayElement->Value.Matrix.Value.BooleanArray[j] is set and can be used here
}
break;
case OpcUaType_SByte:
for (OpcUa_Int32 j = 0; j < numElements; j++)
{
// pArrayElement->Value.Matrix.Value.SByteArray[j] is set and can be used here
}
break;
// all data types have to be handled here ...
}
break;
}
default:
break;
}
}
}
else
{
// unexpected datatype or array type
}