Anatoly Anatoly - 2 months ago 35
C Question

lvalue required as left operand of assignment

example (doesn't work):

#define DIR(mem,d) d==LEFT?mem->left:mem->right


or

#define DIR(mem,d) (d==LEFT?(*mem).left:(*mem).right)


Another structure:

struct Node{
char color;
int height;
Node* parent;
Node* left;
Node* right;
Container*data;
};


In main function i do the following:

Node*temp;
Node*mem=malloc( sizeof(Node) );
d=LEFT;
temp=malloc( sizeof(Node) );
DIR(mem,d) = temp;


Error: lvalue required as left operand of assignment
DIR(mem,d) = temp;


Thank you.

M.M M.M
Answer

The ternary operator does not yield an lvalue in C. One alternative is:

#define DIR(n,d) *((d)==LEFT?&(n).left:&(n).right)

Note that you should parenthesize all uses of macro parameters.