Next Previous Contents

7. Pointers

SDCC allows (via language extensions) pointers to explicitly point to any of the memory spaces of the 8051. In addition to the explicit pointers, the compiler also allows a _generic class of pointers which can be used to point to any of the memory spaces.

Pointer declaration examples.

/* pointer physically in xternal ram pointing to object in internal ram
data unsigned char * xdata p;
/* pointer physically in code rom pointing to data in xdata space */ 
 unsigned char * code p;
/* pointer physically in code space pointing to data in code space */ 
 unsigned char * code p;

/* the folowing is a generic pointer physically located
 in xdata space */
char * xdata p;

Well you get the idea. For compatibility with the previous version of the compiler, the following syntax for pointer declaration is also supported. Note the above examples will be portable to other commercially available compilers.

unsigned char _xdata *ucxdp; /* pointer to data in external ram */ 
 char _data  *ucdp ; /* pointer to data in internal ram */ 
unsigned char _code
  *uccp ; /* pointer to data in R/O code space */
unsigned char _idata *uccp;
  /* pointer to upper 128 bytes of ram */

All unqualified pointers are treated as 3 - byte '_generic' pointers. These type of pointers can also to be explicitly declared.

unsigned char _generic *ucgp;

The highest order byte of the generic pointers contains the data space information. Assembler support routines are called whenever data is stored or retrieved using _generic pointers. These are useful for developing reusable library routines. Explicitly specifying the pointer type will generate the most efficient code. Pointers declared using a mixture of OLD/NEW style could have unpredictable results.

Next Previous Contents