Entering content frame

Access Using Offset and Length Specifications Locate the document in its SAP Library structure

Offset and length specifications are generally critical since the length of each character is platform-dependent. As a result, it is initially unclear as to whether the byte unit or the character unit is referred to in mixed structures. This forced us to put in place certain considerable restrictions. However, access using offset or length specifications is still possible to the degree described in the following. The tasks subject to this rule include accessing single fields and structures, passing parameters to subroutines and working with field symbols.

Single field access

Offset- or length-based access is supported for character-type single fields, strings and single fields of types X and XSTRING. For character-type fields and fields of type STRING, offset and length are interpreted on a character-by-character basis. Only for types X and XSTRING, the values for offset and length are interpreted in bytes.

Structure access

Offset- or length-based access to structured fields is a programming technique that should be avoided. This access type results in errors if both character and non-character-type components exist in the area identified by offset and length.

Offset- or length-based access to structures is only permitted in a UP if the structures are flat and the offset/length specification includes only character-type fields from the beginning of the structure. The example below shows a structure with character-type and non-character-type fields. Its definition in the ABAP program and the resulting assignment in the main memory is as follows:

  a(3)  TYPE C,   "Length: 3 characters
  b(4)  TYPE N,   "Length:  4 characters
  c     TYPE D,   "Length:  8 characters
  d     TYPE T,   "Length:  6 characters
  e     TYPE F,   "Length:  8 bytes
  f(26) TYPE C,   "Length: 28 characters
  g(4)  TYPE X,   "Length: 2 bytes

Internally, the fragment view contains four fragments. Offset- or length-based access in this case is only possible in the first fragment. Statements like struc(21) or struc+7(14) are accepted by the ABAP interpreter and treated like a single field of type C. By contrast, struc+57(2) access is now only allowed in an NUP. If offset-/length-based access to a structure is permitted, both the offset and length specifications are generally interpreted as characters in a UP.

Passing parameters to subroutines

Up to now, parameter passing with PERFORM has allowed you to use cross-field offset and length specifications. In future, this will no longer be allowed in a UP. In a UP, offset and length-based access beyond field boundaries returns a syntax or runtime error. For example, access types c+15 or c+5(10) would trigger such an error for a ten-digit C field c.

If only an offset but no length is specified for a parameter, the entire length of the field instead of the remaining length was previously used for access. As a result, parameter specifications are cross-field if you use only an offset, and therefore trigger a syntax error in a UP. PERFORM test USING c+5 is consequently not permitted.

In addition, in a UP, you can continue to specify the remaining length starting from the offset off for parameters using the form field+off(*).

Ranges for offset and length access when using field symbols

A UP ensures that offset- or length-based access with ASSIGN is only permitted within a predefined range. Normally, this range corresponds to the field boundaries in case of elementary fields or, in case of flat structures, to the purely character-type starting fragment. Using a special RANGE addition for ASSIGN, you can expand the range beyond these boundaries.

Field symbols are assigned a range allowed for offset/length specifications. If the source of an ASSIGN statement is specified using a field symbol, the target field symbol adopts the range of the source. If not explicitly specified otherwise, the RANGE is determined as follows:

ASSIGN feld TO <f>.

In a UP, the field boundaries of field are assigned to <fs> as the range, where field is no field symbol.

ASSIGN <g> TO <f>.

<fs2> adopts the range of <fs1>.

ASSIGN elfeld+off(len) TO <f>.

In a UP, the field boundaries of the elementary field elfield are assigned to <fs> as the range.

ASSIGN <elfld>+off(len) TO <f>.

<fs> adopts the range of the elementary field <elfield>.

ASSIGN struc+off(len) TO <f>.

ASSIGN <struc>+off(len) TO <f>.

In a UP, the purely character-type starting section of the flat structures struc or <struc> determines the range boundaries.

If the assignment to the field symbol is not possible because the offset or length specification exceeds the range permitted, the field symbol is set to UNASSIGNED in a UP. Other checks such as type or alignment checks return a runtime error in a UP. As a rule, offset and length specifications are counted in characters for data types C, N, D, and T as well as for flat structures, and in bytes in all other cases.

Offset without length specification when using field symbols

Up to now, ASSIGN field+off TO <fs> has shown the special behavior that the field length instead of the remaining length of field was used if only an offset but not length was specified. Since an ASSIGN with a cross-field offset is therefore problematic under Unicode, you must observe the following rules:

  1. As previously, the field length of field is used as the length. Using ASSIGN field+off(*)... you can explicitly specify the remaining length.
  2. ASSIGN <fs1>+off TO <fs2> is only permitted if the runtime type of <fs1> is flat and elementary, that is, C, N, D, T (offset in characters), or X (offset in bytes).
  3. ASSIGN field+off TO <fs2> is generally forbidden from a syntactical point of view since any offset <> 0 would cause the range to be exceeded. Exceptions are cases in which a RANGE addition is used.

These rules enable you also in future to pass a field symbol through an elementary field using ASSIGN <fs>+n TO <fs>, as it is the case in a loop, for example.


Note Use the categories used in the Dictionary when using these types with Structure Enhancements.

Leaving content frame