Untyped Pointers

Untyped pointers are declared in the same fashion as standard C pointers. For example:
char *s;	/* string ptr */
int *numptr; /* int ptr */
long *state; /* Texas */

Untyped pointers are always stored using three bytes. The first byte is for the memory type, the second is for the high-order byte of the offset, and the third is for the low-order byte of the offset. The following table contains the memory type byte values and their associated memory type.


Memory Type


idata


xdata


pdata

data,
bdata


code

Value

1

2

3

4

5

Untyped pointers may be used to access any variable regardless of its location in 8051 memory space. Many of the C51 library routines use these pointer types for this reason. By using these generic untyped pointers, a function can access data regardless of the memory in which it is stored.

NOTE
The code generated for an untyped pointer will execute more slowly than the equivalent code generated for a typed pointer. This is because the memory area is not known until run-time. The compiler cannot optimize memory accesses and must generate generic code that can access any memory area. If execution speed is a priority, you should use typed pointers instead of untyped pointers wherever possible.

The following code and assembly listing shows the values assigned to untyped pointers for variables in different memory areas. Note that the first value is the memory space followed by the high-order byte and low-order byte of the address.

.
.
.
stmt level source
1 char *c_ptr; /* char ptr */
2 int *i_ptr; /* int ptr */
3 long *l_ptr; /* long ptr */
4
5 void main (void)
6 {
7 1 char data dj; /* data vars */
8 1 int data dk;
9 1 long data dl;
10 1
11 1 char xdata xj; /* xdata vars */
12 1 int xdata xk;
13 1 long xdata xl;
14 1
15 1 char code cj = 9; /* code vars */
16 1 int code ck = 357;
17 1 long code cl = 123456789;
18 1
19 1
20 1 c_ptr = &dj; /* data ptrs */
21 1 i_ptr = &dk;
22 1 l_ptr = &dl;
23 1
24 1 c_ptr = &xj; /* xdata ptrs */
25 1 i_ptr = &xk;
26 1 l_ptr = &xl;
27 1
28 1 c_ptr = &cj; /* code ptrs */
29 1 i_ptr = &ck;
30 1 l_ptr = &cl;
31 1 }
.
.
.
ASSEMBLY LISTING OF GENERATED OBJECT CODE


; FUNCTION main (BEGIN)
; SOURCE LINE # 5
; SOURCE LINE # 6
; SOURCE LINE # 20
0000 750004 R MOV c_ptr,#04H
0003 750000 R MOV c_ptr+01H,#HIGH dj
0006 750000 R MOV c_ptr+02H,#LOW dj
; SOURCE LINE # 21
0009 750004 R MOV i_ptr,#04H
000C 750000 R MOV i_ptr+01H,#HIGH dk
000F 750000 R MOV i_ptr+02H,#LOW dk
; SOURCE LINE # 22
0012 750004 R MOV l_ptr,#04H
0015 750000 R MOV l_ptr+01H,#HIGH dl
0018 750000 R MOV l_ptr+02H,#LOW dl
; SOURCE LINE # 24
001B 750002 R MOV c_ptr,#02H
001E 750000 R MOV c_ptr+01H,#HIGH xj
0021 750000 R MOV c_ptr+02H,#LOW xj
; SOURCE LINE # 25
0024 750002 R MOV i_ptr,#02H
0027 750000 R MOV i_ptr+01H,#HIGH xk
002A 750000 R MOV i_ptr+02H,#LOW xk
; SOURCE LINE # 26
002D 750002 R MOV l_ptr,#02H
0030 750000 R MOV l_ptr+01H,#HIGH xl
0033 750000 R MOV l_ptr+02H,#LOW xl
; SOURCE LINE # 28
0036 750005 R MOV c_ptr,#05H
0039 750000 R MOV c_ptr+01H,#HIGH cj
003C 750000 R MOV c_ptr+02H,#LOW cj
; SOURCE LINE # 29
003F 750005 R MOV i_ptr,#05H
0042 750000 R MOV i_ptr+01H,#HIGH ck
0045 750000 R MOV i_ptr+02H,#LOW ck
; SOURCE LINE # 30
0048 750005 R MOV l_ptr,#05H
004B 750000 R MOV l_ptr+01H,#HIGH cl
004E 750000 R MOV l_ptr+02H,#LOW cl
; SOURCE LINE # 31
0051 22 RET
; FUNCTION main (END)
.
.
.
In the above example listing, the untyped pointers c_ptr, i_ptr, and l_ptr  are all stored in the internal data memory of the 8051. However, you may specify the memory area in which an untyped pointer is stored by using a memory type specifier. For example:
char * xdata strptr;	/* untyped ptr stored in xdata */
int * data numptr; /* untyped ptr stored in data */
long * idata varptr; /* untyped ptr stored in idata */

These examples are pointers to variables that may be stored in any memory area. The pointers, however, are stored in xdata,  data, and idata  respectively.