
Basic Data Operation

This is a basic data operation. It represents a fundamental action on a basic data type.
You may see other such operations in the Basic Data Operations category, or:


Integer Operations
Arithmetic |
Comparison
Boolean Operations
Bitwise |
Logical
String Operations
Concatenation |
Interpolation |
Comparison |
Matching
Memory Operations
Pointers & references |
Addresses




Task

Demonstrate how to get the address of a variable and how to set the address of a variable.




 
declare addr   builtin; /* retrieve address of a variable               */
declare ptradd builtin; /* pointer addition                             */
declare cstg   builtin; /* retrieve length of the storage of a variable */
declare hbound builtin; /* retrieve the number of elements in an array  */
 
declare p pointer;
declare i bin fixed(31) init(42);
p = addr(i); /* Obtain address of variable, stored in integer variable k */
 
/* how to read a string bit by bit - example for pointerAdd           */
/* we built a pointer (movingPointer), which will move through the    */
/*   storage of a variable (exampleTxt). attached to the pointer is   */
/*   an array of bits (movingBit) - this means wherever the pointer   */
/*   is pointing to, this will also be the position of the array.     */
/* only whole bytes can be addressed. to get down to the single bits, */
/*   an array of 8 bits is used.                                      */
 
declare exampleTxt    char(16) init('Hello MainFrame!);
declare movingPointer pointer;
declare movingBit(8)  bit(01) based(movingPointer);
 
declare walkOffset    bin fixed(31);
declare walkBit       bin fixed(31);
 
do walkOffset = 0 to cstg(exampleTxt)-1;
  movingPointer = ptradd(addr(exampleTxt, walkOffset);
 
  do walkBit = 1 to hbound(movingBit);
    put skip list( 'bit at Byte '  !!walkOffset
                 !!' and position '!!walkBit
                 !!' is '          !!movingBit(walkBit));
  end;
end;
 