Skip to main content

IAR Embedded Workbench for RH850 3.20.x

CERT-INT36-C

In this section:
Synopsis

Converting a pointer to integer or integer to pointer.

Enabled by default

Yes

Severity/Certainty

Low/High

lowhigh.png
Full description

Although programmers often use integers and pointers interchangeably in C, pointer-to-integer and integer-to-pointer conversions are implementation-defined. Conversions between integers and pointers can have undesired consequences depending on the implementation.

Coding standards
CERT INT36-C

Converting a pointer to integer or integer to pointer

Code examples

The following code example fails the check and will give a warning:

void func(unsigned int flag) {
  char *ptr;
  /* ... */
  unsigned int number = (unsigned int)ptr;
  number = (number & 0x7fffff) | (flag << 23);
  ptr = (char *)number;
}

The following code example passes the check and will not give a warning about this issue:

struct ptrflag {
  char *pointer;
  unsigned int flag : 9;
} ptrflag;

void func(unsigned int flag) {
  char *ptr;
  /* ... */
  ptrflag.pointer = ptr;
  ptrflag.flag = flag;
}