3.3.1 Defining Pointer Types with TYPEDEF

Summary: Once defined, a TYPEDEF is considered the same as an intrinsic type.

You can define types for pointer variables using the TYPEDEF directive. A type so defined is considered the same as the intrinsic types provided by the assembler and can be used in the same contexts. The syntax for TYPEDEF when used to define pointers is

typenameTYPEDEF [[distance]] PTR qualifiedtype

The typename is the name assigned to the new type. The distance can be NEAR, FAR, or any distance modifier. The qualifiedtype can be any previously intrinsic or defined MASM type, or a type previously defined with TYPEDEF. (See Section 1.2.6, “Data Types,” for a full definition of qualifiedtype.)

Here are some examples of user-defined types:

PBYTE TYPEDEF PTR BYTE ; Pointer to bytes

NPBYTE TYPEDEF NEAR PTR BYTE ; Near pointer to bytes

FPBYTE TYPEDEF FAR PTR BYTE ; Far pointer to bytes

PWORD TYPEDEF PTR WORD ; Pointer to words

NPWORD TYPEDEF NEAR PTR WORD ; Near pointer to words

FPWORD TYPEDEF FAR PTR WORD ; Far pointer to words

PPBYTE TYPEDEF PTR PBYTE ; Pointer to pointer to bytes

; (in C, an array of strings)

PVOID TYPEDEF PTR ; Pointer to any type of data

STRUCT PERSON ; Structure type

name BYTE 20 DUP (?)

num WORD ?

PERSON ENDS

PPERSON TYPEDEF PTR PERSON ; Pointer to structure type

The distance of a pointer can either be set specifically or determined automatically by the memory model (set by .MODEL) and the segment size (16 or 32 bits). If you don't use .MODEL, near pointers are the default.

In 16-bit mode, a near pointer is two bytes that contain the offset of the object pointed to. A far pointer requires four bytes, and it contains both the offset and the segment. In 32-bit mode, a near pointer is four bytes and a far pointer is six bytes. If you specify the distance with NEAR or FAR, the default distance of the current segment size is used. You can use NEAR16, NEAR32, FAR16, and FAR32 to override the defaults set by the current segment size. In flat model, NEAR is the default.

A pointer type created with TYPEDEF can be used to declare pointer variables. Here are some examples using the pointer types defined above:

; Type declarations

Array WORD 25 DUP (0)

Msg BYTE "This is a string", 0

pMsg PBYTE Msg ; Pointer to string

pArray PWORD Array ; Pointer to word array

npMsg NPBYTE Msg ; Near pointer to string

npArray NPWORD Array ; Near pointer to word array

fpArray FPWORD Array ; Far pointer to word array

fpMsg FPBYTE Msg ; Far pointer to string

S1 BYTE "first", 0 ; Some strings

S2 BYTE "second", 0

S3 BYTE "third", 0

pS123 PBYTE S1, S2, S3, 0 ; Array of pointers to strings

ppS123 PPBYTE pS123 ; A pointer to pointers to strings

Andy PERSON <> ; Structure variable

pAndy PPERSON Andy ; Pointer to structure variable

; Procedure prototype

EXTERN ptrArray:PBYTE ; External variable

Sort PROTO pArray:PBYTE ; Parameter for prototype

; Parameter for procedure

Sort PROC pArray:PBYTE

LOCAL pTmp:PBYTE ; Local variable

.

.

.

ret

Sort ENDP

Once defined, pointer types can be used in any context where intrinsic types are allowed.