Device Monitoring Studio - Monitor, log and analyze data coming through PC ports and connections
Download Device Monitoring Studio Hide this button

Variables and Variable Arrays

Variables work almost like constants with an exception that you can change their value at a later time.


var name-id [= value-expr];

You do not specify the type of the variable. It is automatically determined from the type of the value-expr. You may change the variable type at a later time by assigning another value to a variable.

Variables are allowed at user-defined type scope. If value-expression is omitted, variable is initialized with zero. You can change the value of a variable using the following syntax:

variable-name-id = expression;

Variables may be used in expressions just like constants. For example:

struct A
    var SomeVariable = 10;    // declare variable and assign a value to it
// …
    SomeVariable = SomeVariable * 2 - 20;  // change the value of the variable
// …
    char Data[SomeVariable];    // use variable


Device Monitoring Studio performs an optimization when it compiles variables. All constant expressions are evaluated to their numeric equivalents. Device Monitoring Studio can also optimize constant sub-expressions if they do not have side effects.

struct A
    var MyConstant = 60 * 60;    // will be optimized directly to 3600
    var PI = 3.1415926;
    var _2PI = 2 * PI;        // will be optimized directly to 6.2831852
    int a;
    var test = a * (_2PI / 180);  // sub-expression in parenthesis will be optimized

Using Variables

Variables let you overcome the limitations of standard lookup procedure. A good example is a definition of a PNG file structure (installed with Hex Editor Neo). A PNG file consists of several chunks of different type and size. Although these chunks are almost unrelated, sometimes the structure of a chunk greatly depends on some of the fields of one of the previous chunks. Using variables you may “capture” the value of such field and later use it in subsequent chunks to choose between one structure or another. See the provided sample file for more information on using variables.

Variable Arrays

You can use the following syntax to declare an array:

var name-id [ [total-elements-expr] ] [ = { initializer-list } ];

Where name-id is a name of array, total-elements-expr is an optional number of elements in an array. If omitted, the number of expressions in the initializer-list sets the number of elements in an array.

initialize-list is a comma-separated list of expressions that initialize array elements.