Keil Logo

C166: INCORRECT ADDRESS WHEN INITIALIZING A POINTER


Information in this article applies to:

  • C166 All Versions

SYMPTOMS

I have written the following code:

#define BASE_ADDR (unsigned char *)0x80000 + 0x01;
unsigned char *x;

void main(void)
{
  x = BASE_ADDR;
  *x = 0x55;
}

However, when I execute this, x points to the location 0x00001 not 0x80001. Why is it only assigning my offset of 0x01 to the pointer address?

CAUSE

Your example uses a 16-bit pointer. When you assign an address above 0xFFFF to a 16-bit pointer, the address is truncated to the lower 16 bits. In this case 0x0001.

RESOLUTION

You can do one of two things depending on your application.

  1. Change your pointer to a far, huge or xhuge pointer. All these memory types feature 32-bit pointers so no truncation will take place and you will be able to access the entire address range.  For example:
    #define BASE_ADDR (unsigned char far *)0x80000 + 0x01;
    unsigned char far *x;
    
    void main(void)
    {
      x = BASE_ADDR;
      *x = 0x55;
    }
    
  2. Change to a memory model where the default variable type is far or huge. For example:
    • COMPACT
    • HCOMPACT (not for 166 CPU)
    • LARGE
    • HLARGE (not for 166 CPU)

MORE INFORMATION

Last Reviewed: Friday, July 15, 2005


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  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.