ARM Technical Support Knowledge Articles

INCORRECT ADDRESS WHEN INITIALIZING A POINTER

Applies to: C166 C Compiler

Answer


Information in this article applies to:


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

Article last edited on: 2005-07-15 13:12:29

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential