It's legal and sleazy enough that I might have done it...
I'd almost say it's doing nothing, because message_t*
already includes the message header. But a serial_header
may be a different size (hopefully smaller...).
msg->data is an array of chars and thus already a pointer
(I'm not sure, but putting an & in front of it might just
be a noop in this case anyway...). sizeof() is returning
something that is the moral equivalent of an int, and
subtracting that from a char* just backs the pointer
up that number of bytes. The whole thing is counting
on data[] being chars because pointer arithmetic is
done with an implicit sizeof(element) multiplier.
Of your other suggested options, this might be one
way to do it:
> (uint8_t *)msg + offsetof(message_t, data)
That gets you to the msg->data pointer again and
enforces that you are working with bytes. But you
might as well go whole hog and subtract the
serial_header size at the same time:
(uint8_t *)msg + offsetof(message_t, data) - sizeof(serial_header_t);
Isn't C fun?
MS
Flemming Nyboe wrote:
> Hello,
>
>
>
> Is this legal:
>
> --- 8< ----
>
> serial_header_t* getHeader(message_t* msg) {
>
> return (serial_header_t*)(msg->data - sizeof(serial_header_t));
>
> }
>
> --- 8< ----
>
> … or is the (msg->data) missing an address-of operator?
>
> The construct is found (at least) in TEP111 and CC2420TimeSyncMessageP.ncf
>
>
>
> BR Flemming Nyboe
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tinyos-help mailing list
> [email protected]
> https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help
_______________________________________________
Tinyos-help mailing list
[email protected]
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help