Reputation: 3038
I am trying to understand a line of C-code which includes using a pointer to struct value (which is a pointer to something as well).
// Given
typedef struct {
uint8 *output
uint32 bottom
} myType;
myType *e;
// Then at some point:
*e->output++ = (uint8) (e->bottom >> 24);
Source: https://www.rfc-editor.org/rfc/rfc6386#page-22
Upvotes: 0
Views: 101
Reputation: 13196
The line
*e->output++ = (uint8) (e->bottom >> 24);
does the following:
bottom
of the structure pointed to by the pointer e
.uint8_t
, which now contains the high order byte.output
of the structure. It's a pointer to uint8_t
.uint8_t
we computed earlier into the address pointed to by output
.output
, causing it to point to the next uint8_t
.The order of some of those things might be rearranged a bit as long as the result behaves as if they had been done in that order. Operator precedence is a completely separate question from order in which operations are performed, and not really relevant here.
Upvotes: 1
Reputation: 25605
"What exactly does that line of C-code do?"
Waste a lot of time having to carefully read it instead just knowing at a glance. If I was doing code review of that, I'd throw it back to the author and say break it up into two lines.
The two things it does is save something at e->output, then advance e->output to the next byte. I think if you need to describe code with two pieces though, it should be on two lines with two separate statements.
Upvotes: 3
Reputation: 3038
As pointed out by Deduplicator in the comments above, looking at an operator precedence table might help.
*e->output++ = ...
means "assign value ...
to the location e->output
is pointing to, and let e->output
point to a new location 8 bits further afterwards (because output
is of type uint8
). (uint8) (e->bottom >> 24)
is then evaluated to get a value for ...
Upvotes: 1