CCS does not monitor this forum on a regular basis. Please do not post bug reports on this forum. Send them to support@ccsinfo.com
Author Message
maxrate

Joined: 01 Jan 2007
Posts: 34

Jerson

Joined: 31 Jul 2009
Posts: 122
Location: Bombay, India

Posted: Wed Oct 18, 2023 8:05 pm

 Quote: #define hi(x) (*((int8 *)&x+1))

Try to read it like this

define macro

hi of x
as
the value pointed at (*
which is of type int8 pointer (int8 *)
whose location is address x+1 (&x+1)

Hope you can now understand it.
maxrate

Joined: 01 Jan 2007
Posts: 34

 Posted: Thu Oct 19, 2023 5:10 am Jerson, this helps a lot, thank you. I do have a question... is the 'x' type defined by (* or (int8 *) portion of the snippet?
Ttelmah

Joined: 11 Mar 2010
Posts: 19215

Posted: Thu Oct 19, 2023 5:21 am

Neither.

X is just any variable. It's type is what it is declared as.

So:
 Code: int16 fred=0xaa55;    int8 val;    val=hi(fred);

Will have val holding 170 (0xaa).

You can actually use 'hi' defined this way on any variable.
On an int8, it will return garbage - pulling whatever is in the next
variable in memory. However on any other variable, it will return what
is in the second byte of the variable.

Macros fundamentally are 'type less'.

The key point about the int8 *, is it casts the address of 'x' to be of
type int8 *. This then means when you increment it, it increments by
just one. Pointers generically increment by the size of the variable
they point to.
maxrate

Joined: 01 Jan 2007
Posts: 34

 Posted: Sun Oct 22, 2023 5:43 pm Thank you Ttelmah and Jerson, I believe this to be crystal clear now. I didn't consider using this macro/function on an int8, as it is designed to retrieve the more significant portion of the int16 and cast to an int8. I agree that would pull garbage if used against an int8. Really fantastic of you both to offer some assistance.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT - 6 Hours Page 1 of 1

 Jump to: Select a forum Software----------------General CCS C DiscussionCode LibraryEZ App LynxBest OfKnown Issues Hardware----------------CCS ICD / Mach X / Load-n-Go
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum