Keil Logo

Pointers with Memory Types

Memory types may used in pointer declarations to define the memory area of the object referenced or even the memory area where the pointer is located. In general, pointers should be declared according to the following rules:

Variable Declaration Pointer Size Pointer Declaration
char c; 16/32-bit char *ptr;
(Pointer size depends on the memory model in use)
int near nc; 16-bit int near *np;
float sdata nf; 16-bit float sdata *sp;

or

float near *sp;
long bdata x; 16-bit long bdata *xp;

or

long near *xp;
char idata index; 16-bit char idata *ip;

or

char near *ip;
const char near n = 5; 16-bit char near *cp;
(const may be ignored)
unsigned long far l; 32-bit long far *lp;
char huge hc; 32-bit char huge *hc_ptr;
long xhuge xl; 32-bit char xhuge *hl_ptr;
void near func1 (void); 16-bit void (near *fp1) (void);
int far func2 (void); 32-bit int (far *fp2) (void);

Note

  • Pointers to sdata, idata, or bdata objects may so be declared with the near memory type because a near pointer can hold the DPP for all objects that have a logical 16-bit address.
  • In the TINY model, the memory type for pointer declarations are unnecessary because each object is accessed via a 16-bit address.
  • near objects can be addressed with a far, huge, or xhuge pointer. The far, huge, or xhuge pointer access always yields DPP load or EXTS/EXTP instructions.
  • A near pointer access uses the pre-loaded DPP registers and cannot be used to address a far, huge, or xhuge object.

The following examples illustrate several different declaration types:

char near *px

This example declares a pointer that refers to a char type object in near memory. The pointer is stored in the default memory area. The *px pointer has a size of 2 bytes.

char near *idata pdx

This example is similar to the char near *px except the pointer is placed into the internal data memory (idata) regardless of the memory model used.

struct time { char hour; char min; char sec; struct time near *pxtime; }

This example contains a pointer, pxtime, that refers to another structure that must be located in the near memory type (near, idata, sdata, or bdata). The pxtime pointer has a size of 2 bytes.

struct time far *ptime

This example defines a pointer stored in the default memory type and refers to a structure time located in the far memory type. The ptime pointer has a size of 4 bytes.

ptime->pxtime->hour = 12

This example uses the prior two declarations; however, the pxtime pointer is indirectly loaded from the structure. The pxtime pointer 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.