Reputation: 19
I have defined a struct named wire:
typedef struct wire
{
char type[9];
char name[20];
char value;
struct io * inp=NULL;
struct io * outp=NULL;
} wire;
and another named io:
typedef struct io
{
struct io* next;
struct log_gate* conn_gate ;
} io;
wires is a dynamic array of wire.
and compile error occurs in this line:
tmp=&wires[i]->outp;
The compile error is: base operand of '->' has non-pointer value 'wire'
Upvotes: 0
Views: 119
Reputation: 311186
Write either
tmp = ( &wires[i] )->outp;
or
tmp = wires[i].outp;
If wires
is a pointer to allocated memory of an array then wires[i]
is an element of the array and has type struct wire
that is it is not a pointer. So the correct syntax will be as the second example of statement shown above.
If you uses the address of this element like &wires[i]
then unary operator &
has lower priority than postfix operator ->
. So you need to enclose expression &wires[i]
in parentheses (&wires[i])
like it is shown in the first example of statement above that to get a pointer.
Upvotes: 2
Reputation: 6831
First we must take a look at the precedence of both operators in question. According to this chart, ->
has a higher precedence than &
. This means that we must evaluate the ->
before the &
. Lets take a look at the expression.
wires[i] /// this returns a wire
Due to the precedence of ->
we now evaluate the struct accessor operation.
wires[i]->outp /// because wires[i] is a wire, not a wire *, the -> is misused.
wires[i]
is just a plain old wire
, not a pointer, so we must use the .
operator to access its members. Remember, ->
vs .
is determined by the type of the struct, not the type of the member. Because the struct is not a pointer, we use .
The precedence issue is important because if &
had a higher precendence than ->
, then the expression &wires[i]
would be evaluated first. This would mean that we are trying to get at the outp
field of a wire *
instead of a wire
, and the ->
operator would be justified.
Upvotes: 2