%STR Used to Store Null-Terminated String
%STR Used to Store Null-Terminated String
When used on the left-hand side of an expression, %STR(ptr:length) assigns the value of the right-hand side of the expression to the storage pointed at by the pointer, adding a null-terminating byte at the end. If the length specified as the second parameter of %STR is N, then at most N-1 bytes of the right-hand side can be used, since 1 byte must be reserved for the null-terminator at the end.
The maximum length that can be specified is 16773104. This means that at most 16773103 bytes of the right-hand side can be used, since 1 byte must be reserved for the null-terminator at the end.
The length indicates the amount of storage that the pointer points to. This length should be greater than the maximum length the right-hand side will have. The pointer must be set to point to storage at least as long as the length parameter. If the length of the right-hand side of the expression is longer than the specified length, the right-hand side value is truncated.
Warning: Data corruption will occur if both of the following are true:
- The length parameter is greater than the actual length of data addressed by the pointer.
- The length of the right-hand side is greater than or equal to the actual length of data addressed by the pointer.
If you are dynamically allocating storage for use by %STR, you must keep track of the length that you have allocated.
Figure 1. %STR (left-hand-side) Examples
*..1....+....2....+....3....+....4....+....5....+....6....+....7...+....
D String1 S *
D Fld1 S 10A
/FREE
%str(String1: 25)= 'abcdef';
// The storage pointed at by String1 now contains 'abcdef¬'
// Bytes 8-25 following the null-terminator are unchanged.
%str (String1: 4) = 'abcdef';
// The storage pointed at by String1 now contains 'abc¬'
/END-FREE