I've seen this example in a text book and am a little confused how to interpret the operator precedence rules. Given this struct:
typedef struct {
char *data;
size_t start, end;
} str_wends;
Let B
be declared as a pointer to this struct,that is.
str_wends *B = malloc(sizeof(str_wends));
and assume B->start != 0
,
then this should be a legal construct:
&B->data[B->start]
which will return a reference to the string offset by B->start
. The question is how to interpret the precedence rules. Since we know that ->
and []
have the same precedence and bind left to right, why is B->Start
evaluated before B->data[]
.
and assume B->start != 0,
It is legal even ifB->start
==0
.why is B->Start evaluated before B->data[]
-- Because you need the result ofb->start
beforeb->data[]
can be evaluated.b->data
first, thenb->start
, and as last step, using the result of the already evaluatedb->data
andb->start
to evaluate[*]
. I am not sure but i think this is undefined behavior since it is almost the same problem here as with function-parameter evaluation.B->data + B->start
) is clearer and doesn't raise questions about precedence. Please cite the textbook, because the example, while valid, is pretty poor practice.