Memory Type in Pointer Declarations
The memory type may be used in pointer declarations to define the pointer size. A pointer without explicit memory type is called a generic pointer. Pointers with explicit memory type are called memory specific pointers. A memory specific pointer allows for each variable access the generation of efficient code.
In general, pointers should be declared according to the following rules:
| Variable Declaration | Pointer Size | Declaration of the Pointer |
|---|
| char c; | 16/32 bit | char *ptr; (Pointer size depends from the memory model in use.) |
| int near nc; | 16 bit | int near *np; |
| float data df; | 8 bit | float data *dp; /* or */ float near *dp; |
| char idata index; | 16 bit | char idata *ip; /* or */ char near *ip; |
| long xdata x; | 16 bit | long xdata *xp; |
| const char code c = 'A'; | 16 bit | char code *cp; /* const can be ignored */ |
| const char near n = 5; | 16 bit | char near *np; /* const can be ignored */ |
| unsigned long far l; | 32 bit | long far *lp; |
| char huge hc; | 32 bit | char huge *hp_ptr; |
| void near func1 (void); | 16 bit | void (near *fp1) (void); |
| int far func2 (void); | 32 bit | int (far *fp2) (void); |
Note
- Pointers to data or idata objects may be declared using the near memory type since a near pointer can also access data or idata memory.
- In the TINY and XTINY memory model the default pointer size is 16 bit and the default memory type for pointers is near *. To access memory areas above 0x010000, you must specify the memory type far * or huge * in the pointer declaration.
- With a far or huge pointer, near, data or idata objects may be addressed. The difference is that far or huge pointer accesses are using the @DRk instructions, whereas a near pointer uses the more efficient @WRj instructions. A far, huge or xhuge object cannot be addressed with a near pointer.
Declaration Examples
char near *px;
declares a pointer which refers to an object of the type char in near memory type. The pointer itself is stored in the default memory type (depends on the memory model in use) and has a size of 2 bytes.
char near *data pdx;
this declaration corresponds to the prior declaration except that the pointer itself is placed explicitly into the on-chip data memory (data), independent of the memory model used.
int (* xdata fp) (void);
this declaration defines a function pointer that is placed explicitly into the xdata memory (xdata), independent of the memory model used. The function addressed is from the type void func (void).
struct time { char hour; char min; char sec; struct time near *pxtime; };
besides other elements, the structure shown contains a pointer pxtime to another structure which must located in the near memory type. The pointer pxtime has a size of 2 bytes.
struct time far *ptime;
this declaration defines a pointer which is stored in the default memory type and refers to a structure time which is located in the far memory type. The pointer ptime requires 4 bytes.
ptime->pxtime->hour = 12;
use of the prior two declarations; the pointer pxtime is indirectly loaded from the structure. It refers to the structure time which is located in the near memory type. The value 12 is assigned to the member hour in this structure.