Chris Howarth
Chris Howarth

Reputation: 33

Can someone explain this line of c++ code using reinterpret_cast?

Consider (1):

 uint8_t *pUART = reinterpret_cast<uint8_t*>(0x0800);

I know (1) simply changes the pUART pointer to 0x0800, but i'm confused how this way of doing it works.

It would make sense if it was (2):

 uint8_t* pUART = reinterpret_cast<uint8_t*>(0x0800);

Im confused because the (1) is the same as (3):

 uint8_t x = reinterpret_cast<uint8_t*>(0x0800);

but the compiler does not accept this. Can someone clear this up for me? Why does (1) work but not (3).

Upvotes: 3

Views: 155

Answers (2)

suicidalMonkey
suicidalMonkey

Reputation: 1

Line (3) doesn't work, as you're trying to assign uint8_t* pointer type to just uint8_t. The correct variant is your line (1).

However, if you're going with <cstdint> you can also use the uintptr_t type which is an unsigned int pointer.

Upvotes: 0

user743382
user743382

Reputation:

C++ is a tokenised language. That means whitespace does not matter, except where necessary to separate tokens.

uint8_t *pUART and uint8_t* pUART mean exactly the same thing. They are three tokens, namely uint8_t, *, and pUART.

Upvotes: 10

Related Questions