Skip to main content

IAR Embedded Workbench for RL78 5.20

Pointer types

In this section:

The compiler has two basic types of pointers: function pointers and data pointers. Pointer types have the same alignment as the corresponding integer type.

Casting

Casts between pointers have these characteristics:

  • Casting a value of an integer type to a pointer of a smaller type is performed by truncation

  • Casting a value of an integer type to a pointer of a larger type is performed by zero extension

  • Casting a pointer type to a smaller integer type is performed by truncation

  • Casting a pointer type to a larger integer type is performed by first casting the pointer to the largest pointer of the same type that fits within the integer type. After that, it is zero-extended if needed.

  • Casting a data pointer to a function pointer and vice versa is illegal

  • Casting a function pointer to an integer type gives an undefined result

  • Casting a __near pointer to a __far or a __huge pointer is performed by first zero-extending the pointer and then adding 0xF0000 to it

  • Casting a __far or a __huge pointer to a __near pointer is an illegal operation

  • Casting a __near_func pointer to a __far_func pointer is performed by zero extension

  • Casting a __far_func pointer to a __near_func pointer is an illegal operation

  • Casting a __far pointer to a __huge pointer results in the same bit pattern

  • Casting a __huge pointer to a __far pointer is an illegal operation.

size_t

size_t is the unsigned integer type of the result of the sizeof operator. In the IAR C/C++ Compiler for RL78, the type used for size_t is unsigned int.

ptrdiff_t

ptrdiff_t is the signed integer type of the result of subtracting two pointers. In the IAR C/C++ Compiler for RL78, the type used for ptrdiff_t is the signed integer variant of the size_t type.

Note that subtracting pointers other than default pointers could result in a smaller or larger integer type. In each case, this integer type is the signed integer variant of the corresponding size_t type.

Note

It is sometimes possible to create an object that is so large that the result of subtracting two pointers in that object is negative. See this example:

char buff[60000];           /* Assuming ptrdiff_t is a 16-bit */
char *p1 = buff;            /* signed integer type. */
char *p2 = buff + 60000;
ptrdiff_t diff = p2 - p1;   /* Result: -5536 */
intptr_t

intptr_t is a signed integer type large enough to contain a void*. In the IAR C/C++ Compiler for RL78, the type used for intptr_t is signed long.

uintptr_t

uintptr_t is equivalent to intptr_t, with the exception that it is unsigned.