Addition of long values show different o/p

4

I am facing problem in doing addition of long values example

 typedef unsigned short  UINT16;

   UINT16* flash_dest_ptr;  // this is equal to in hexa 0XFF910000

   UINT16 data_length ; // hex =  0x000002AA &  dec = 682

  //now when I add  

  UINT16 *memory_loc_ver = flash_dest_ptr + data_length ;

  dbug_printf( DBUG_ERROR | DBUG_NAVD, " ADD hex =0x%08X\n\r",memory_loc_ver   );

Actual O/p = 0xFF910554

// shouldn't o/p be FF9102AA ?
c++
c
embedded
asked on Stack Overflow May 10, 2013 by samprat • edited Jul 16, 2017 by Cœur

2 Answers

5

It's pointer arithmetic, so

UINT16 *memory_loc_ver = flash_dest_ptr + data_length ;

advances flash_dest_ptr by data_length * sizeof (UINT16) bytes.

Typically, sizeof (UINT16) would be 2, and

2 * 0x2AA = 0x554
answered on Stack Overflow May 10, 2013 by Daniel Fischer
0

When you add integers to a pointer value, you are actually moving the pointer as many bytes as it would take to move data_length UINT16s away in memory, not data_length bytes.

answered on Stack Overflow May 10, 2013 by Collin

User contributions licensed under CC BY-SA 3.0