Typed Pointers

Typed pointers always include a memory type specification in the pointer declaration and always refer to a specific memory area. For example:
char data *str;	/* ptr to string in data */
int xdata *numtab; /* ptr to int(s) in xdata */
long code *powtab; /* ptr to long(s) in code */

Because the memory type is specified at compile-time, the memory type byte required by untyped pointers is not needed by typed pointers. Typed pointers can be stored using only one byte (idata, data, bdata, and pdata pointers) or two bytes (code and xdata pointers).

NOTE
The code generated for a typed pointer will execute more quickly than the equivalent code generated for an untyped pointer. This is because the memory area is known at compile-time rather than run-time. The compiler can use this information to optimize memory accesses. If execution speed is a priority, you should use typed pointers instead of untyped pointers wherever possible.

Like untyped pointers, you may specify the memory area in which a typed pointer is stored. To do so, prefix the pointer declaration with a memory type specifier. For example:
char data * xdata str;	/* ptr in xdata to data char */
int xdata * data numtab; /* ptr in data to xdata int */
long code * idata powtab; /* ptr in idata to code long */

Typed pointers may be used to access variables in the declared 8051 memory area only. Typed pointers provide the most efficient method of accessing data objects, but at the cost of reduced flexibility.

The following code and assembly listing shows how pointer values are assigned to typed pointers. Note that the code generated for these pointers is much less involved than the code generated in the untyped pointers example listing in the previous section.

.
.
.
stmt level source
1 char data *c_ptr; /* typed char ptr */
2 int xdata *i_ptr; /* typed int ptr */
3 long code *l_ptr; /* typed long ptr */
4
5 long code powers_of_ten [] =
6 {
7 1L,
8 10L,
9 100L,
10 1000L,
11 10000L,
12 100000L,
13 1000000L,
14 10000000L,
15 100000000L
16 };
17
18 void main (void)
19 {
20 1 char data strbuf [10];
21 1 int xdata ringbuf [1000];
22 1
23 1 c_ptr = &strbuf [0];
24 1 i_ptr = &ringbuf [0];
25 1 l_ptr = &powers_of_ten [0];
26 1 }
.
.
.
ASSEMBLY LISTING OF GENERATED OBJECT CODE


; FUNCTION main (BEGIN)
; SOURCE LINE # 18
; SOURCE LINE # 19
; SOURCE LINE # 23
0000 750000 R MOV c_ptr,#LOW strbuf
; SOURCE LINE # 24
0003 750000 R MOV i_ptr,#HIGH ringbuf
0006 750000 R MOV i_ptr+01H,#LOW ringbuf
; SOURCE LINE # 25
0009 750000 R MOV l_ptr,#HIGH powers_of_ten
000C 750000 R MOV l_ptr+01H,#LOW powers_of_ten
; SOURCE LINE # 26
000F 22 RET
; FUNCTION main (END)
.
.
.