void InsertA(SET *A,int elem)
{
if( isMember(*A,elem) == false)
{
*A = *A || 1<<elem;; /*it says its in this row*/
}
}
/*Error: Lvalue required in Function InsertA any thoughts on this guys? noob here
*/
In this statement :
*A = *A || 1<<elem;; /*it says its in this row*/
We have these operators *,=,||,<<
Now look at the precedence table at
Precedence Operator operation associativity
-------- --------- ----------------
3 * Indirection (dereference) R to L
7 << Bitwise left shift L to R
14 || Logical OR L to R
16 = Direct assignment R to L
So lets see what happens:
1) Indirection will be performed first. There are two of them. They associate Right to Left. That means Right one will be performed first. Its important to understand that there are two dereferencing operator here which will be considered differently later when encountering the =
operator.
2) A bit wise left shift will performed on 1.
3) A logical OR will be performed with *A
and the result of bitwise shift. it may evaluate zero or non zero.
4) This zero/nonzero value will be assigned to *A
. Here *A
can be treated as lvalue in a context of =
operator. If you leave this consideration it will lead to ambiguity. Because we often think of dereferencing operation like *A
as an rvalue
or an value
to be used. Actually its a valid lvalue
which will be converted implicitly to a rvalue
(This is when a value
which is stored at address pointed by A
is returned). Otherwise *A
is simply a container in memory which is open to values.
So the thing is your expression is undefined and does not make any sense why you are putting a logical value into *A
. It will make more sense if you use binary or
instead of logical.
Lets do that:
We have a new entry in our precedence table
Precedence OP OPeration Associativity
12 | Bitwise OR L to R
Only change will occur in step 3 when a bitwise OR will be performed.
Lets have an example
lets say elem = 3.
A is pointing to the array {1,2,3,3,4}
1) '*A's will be performed. It will just calculate the "Offsets" needed to do load
or store
instructions of the processor.
2)we will get a constant bit pattern : 1 << 3 = 1000
3)now for |
we need rvalues
as both operands. So now a load
instruction will be performed to fetch the value stored in the memory. Say its 2
. So we will get 0010 | 1000 = 1010
4)A store instruction will be performed to put this bit pattern into the memory so the array will look like {1,A,3,3,4}
Explanation for too much verbosity: I think this can help if future users who will try to find how to dissect a complicated expression by language rules.