LEN function

Purpose

Return the logical length of a variable, User-Defined Type, or Union.

Syntax

y& = LEN(target)

Remarks

If target is a string variable or a string expression, LEN returns a value from 0 to the current string length, representing the number of characters in target.  If target is a fixed-length string, the length of the fixed buffer is returned.  If target is an ASCIIZ string, the length of the data stored in the ASCIIZ string is returned, not the maximum size of the ASCIIZ string.  Use SIZEOF to determine the maximum size of an ASCIIZ string.

When used with pointers, LEN returns a value of 4, since a pointer is always stored as a DWORD.  You can use LEN with the target of a pointer to return the size of target.  If the target is a dynamic string, you will receive the length of the string, not the length of the handle.

target can also be any other variable type, including numeric and User-Defined Types (defined with TYPE/END TYPE). In that case, PowerBASIC will return the number of bytes needed to store a variable of that type.

When measuring the size of a padded (aligned) UDT structure with the LEN (or SIZEOF) statement, the measured length includes any padding that was added to the structure.  For example, the following UDT structure:

TYPE LengthTestType DWORD

  a AS INTEGER

END TYPE

' code here

DIM abc AS LengthTestType

x& = LEN(abc)

Returns a length of 4 bytes in x&, since the UDT was padded with 2 additional bytes to enforce DWORD alignment.  Note that the LEN of individual UDT members returns the true size of the member without regard to padding or alignment.  In the previous example, LEN(abc.a) returns 2.

See also

CHRBYTES, SIZEOF

Example

DIM p AS BYTE POINTER

ByteLen = LEN(p)  ' size of a pointer = 4 bytes

ByteLen = LEN(@p) ' size of byte (target) = 1 byte