collapse

Author Topic: C++ coding question  (Read 2225 times)

fad0

  • Member
  • ****
  • f
  • Posts: 20
    • My Robot Blog
C++ coding question
« on: November 01, 2015, 01:31:52 AM »
I just got my Pixy and am in the process of integrating it onto Endeaver, my son's BOE bot.  I am reviewing the code on the Pixy wiki and came across this:

Code: [Select]
for (i=0, sum=0; i<sizeof(Block)/sizeof(uint16_t); i++)
    {
      if (g_blockType==NORMAL_BLOCK && i>=5) // no angle for normal block
      {
        block->angle = 0;
        break;
      }
      w = getWord();
      sum += w;
      *((uint16_t *)block + i) = w;
    }

Can someone explain
Code: [Select]
*((uint16_t *)block + i) = w; ?

Thanks.
Fad0

deshipu

  • Member
  • *
  • Posts: 113
  • Radomir Dopieralski
    • Homepage
Re: C++ coding question
« Reply #1 on: November 01, 2015, 05:24:49 AM »
Code: [Select]
*((uint16_t *)block + i) = w;
Let's do it step by step:

Code: [Select]
(uint16_t *)block
This means "treat the variable 'block' as if it was of type 'uint16_t *'", where "uint16_t *" means "a pointer to uint16_t".

Code: [Select]
(uint16_t *)block + i
Incrementing a pointer to a certain type moves it by the size of the type being pointed to in memory. So this means "make a pointer that points i * 2 bytes further" (16 bits = 2 bytes).

And finally:
Code: [Select]
*((uint16_t *)block + i)
This is pointer dereference, so it says "take whatever the pointer is pointing to".

In summary, the whole line could be possibly written as:

Code: [Select]
((uint16_t *)block)[i] = w;

fad0

  • Member
  • ****
  • f
  • Posts: 20
    • My Robot Blog
Re: C++ coding question
« Reply #2 on: November 01, 2015, 07:18:18 AM »
 :)
Very clear explanation. Thanks deshipu! 

 

* Search


* Recent Topics

The unnamed (yet) quatruped spider project by tinhead
[July 01, 2020, 04:22:11 PM]


"1984 Nixie Time" by 1 what
[May 08, 2020, 01:04:18 AM]


2D Side Scroller Cyberpunk themed by Killer Angel
[February 06, 2020, 06:39:40 AM]


A new wing design for model aircraft / drones by OddBot
[February 06, 2020, 04:42:06 AM]


SDR (Software Defined Radio) by Gareth
[February 02, 2020, 06:15:42 AM]


Circuit Math by ZeroMax
[January 31, 2020, 01:50:18 PM]


NanOMeter by Protowrxs
[January 01, 2020, 12:59:44 PM]


Investigating the VL53L0X Laser Rangefinder by erco
[December 30, 2019, 10:45:44 PM]


PS4 Single Handed Controller Deployed (part 7 of 7) by Gareth
[December 30, 2019, 09:52:29 AM]


"D" -Pad Workio just like Magic (Will Merlin stay or Go) (part 6 of 7) by Gareth
[December 30, 2019, 09:51:27 AM]


PS4 Joystick Digitals 4,5,6,7,10 - Analog's Lx,Ly,Rx,Ry Workio (part 5 of 7) by Gareth
[December 30, 2019, 09:50:37 AM]


Menu Workio ! (part 4 of 7) by Gareth
[December 30, 2019, 09:49:49 AM]


L1 trigger design Workio (Hori controller) (part 3 of 7) by Gareth
[December 30, 2019, 09:48:50 AM]


Hori aka PS4 Joystick Mappings (part 2 of 7) by Gareth
[December 30, 2019, 09:47:17 AM]


PS4 Single Left-Handed Controller (part 1 of 7) by Gareth
[December 30, 2019, 09:44:58 AM]

* Recent Posts

Re: The unnamed (yet) quatruped spider project by tinhead
[July 01, 2020, 04:22:11 PM]


Re: The unnamed (yet) quatruped spider project by jinx
[July 01, 2020, 04:06:19 PM]


Re: "1984 Nixie Time" by 1 what
[May 08, 2020, 01:04:18 AM]


Re: "1984 Nixie Time" by tomasp
[April 13, 2020, 06:03:28 PM]


Re: 2D Side Scroller Cyberpunk themed by Killer Angel
[February 06, 2020, 06:39:40 AM]


A new wing design for model aircraft / drones by OddBot
[February 06, 2020, 04:42:06 AM]


Re: "1984 Nixie Time" by Gareth
[February 02, 2020, 06:23:01 AM]


Re: SDR (Software Defined Radio) by Gareth
[February 02, 2020, 06:15:42 AM]


Re: SDR (Software Defined Radio) by ZeroMax
[January 31, 2020, 01:54:21 PM]


Re: "1984 Nixie Time" by ZeroMax
[January 31, 2020, 01:52:29 PM]


Circuit Math by ZeroMax
[January 31, 2020, 01:50:18 PM]


Re: 2D Side Scroller Cyberpunk themed by ZeroMax
[January 31, 2020, 01:45:33 PM]


NanOMeter by Protowrxs
[January 01, 2020, 12:59:44 PM]


Re: Investigating the VL53L0X Laser Rangefinder by erco
[December 30, 2019, 10:45:44 PM]


PS4 Single Handed Controller Deployed (part 7 of 7) by Gareth
[December 30, 2019, 09:52:29 AM]