Keil Logo

Memory Type in Pointer Declarations

The memory type can 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 generates more efficient code for each variable access.

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 can be declared using the near memory type, because 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 or huge object cannot be addressed with a near pointer.
Declaration Examples

char near *px;
declares a pointer that refers to an object of 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) independently of the memory model in use.

int (* xdata fp) (void);
this declaration defines a function pointer that is placed explicitly into the xdata memory type independently of the memory model in use. The function addressed is of type void func (void).

struct time { char hour; char min; char sec; struct time near *pxtime; };
besides other elements, this structure contains a pointer pxtime to another structure, which must be 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.

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.