1

I'm trying to assign a variable using the result of a function.

MAT2 _m_pow2(const MAT2 A, int p, MAT2 tmp)
{
  MAT2 out;
  int it_cnt, k, max_bit;
#define Z(k) (((k) & 1) ? tmp : out)

  out = m_get2(A.m, A.n);
  tmp = m_get2(A.m, A.n);
  if(p == 0)
    out = m_ident2(out);
  else if(p > 0)
  {
    it_cnt = 1;
    for(max_bit = 0; ; max_bit++)
      if((p >> (max_bit+1)) == 0)
        break;
      tmp = m_copy2(A);
      for(k = 0; k < max_bit; k++)
      {
        Z(it_cnt+1) = m_mlt2(Z(it_cnt), Z(it_cnt));
        it_cnt++;
        if(p & (1 << (max_bit-1)))
        {
          Z(it_cnt+1) = m_mlt2(A, Z(it_cnt));
          it_cnt++;
        }
        p <<= 1;
      }
      if(it_cnt & 1)
        out = m_copy2(Z(it_cnt));
  }
  return out;
#undef Z
}

The function m_mlt2() returns MAT2. I am getting these errors:

error: lvalue required as left operand of assignment

Z(it_cnt+1) = m_mlt2(Z(it_cnt), Z(it_cnt));

and

error: lvalue required as left operand of assignment

Z(it_cnt+1) = m_mlt2(A, Z(it_cnt));

What am I doing wrong? If it needs more detais, please, feel free to ask. Thanks in advance!

P.S. I know I will get downvotes, I tried to do my best formulating this question.

9
  • Welcome to the site! Check out the tour and the how-to-ask page for more about asking questions that will attract quality answers. You can edit your question to include more information. See this answer for one possibility.
    – cxw
    Commented Feb 15, 2018 at 19:35
  • 2
    Try making this into a minimal reproducible example, please! <3 Commented Feb 15, 2018 at 19:36
  • 2
    Your Z macro does not generate an lvalue, as the error says.
    – vanza
    Commented Feb 15, 2018 at 19:37
  • 4
    Could use #define Z(k) (((k) & 1) ? &tmp : &out) ... *Z(it_cnt+1) = m_mlt2(A, *Z(it_cnt)); it should compile and sadly win the wrath of other programmers. Commented Feb 15, 2018 at 19:42
  • @machine_1I forgot to remove that. Thanks! Commented Feb 15, 2018 at 19:55

2 Answers 2

1

(((k) & 1) ? tmp : out) is not an l-value. The result is the value of either tmp, or out, NOT the variable name.

You might try something like this;

  MAT2 out[2];
  int it_cnt, k, max_bit;
  out[1] = tmp;
#define Z(k) out[(((k) & 1))]
1
  • A good alternative if MAT2 is a pointer or simple type/structure. Commented Feb 15, 2018 at 19:53
1

The Z macro expands to a ternary expression. So:

Z(it_cnt+1) = m_mlt2(Z(it_cnt), Z(it_cnt));

expands to (in part):

(((it_cnt+1) & 1) ? tmp : out) = m_mlt2(...);

As a result, you have a ternary expression ( ? : ) on the left side of the assignment =.

An lvalue is (very roughly speaking) a value you can assign to. As this answer points out, in C (not C++), the ternary operator does not produce an lvalue. Instead, as @cleblanc mentioned, it gives you the values of the expressions.

Edit This is C++ code, so your best bet is probably to switch to C++. An example modified from this question:

#include<stdio.h>

int main()
{
 int a=5,b=6;
 ( ((a>b) & 1) ? a : b ) = 42;
 printf("%d %d\n", a, b);
 return 0;
}

In C:

$ gcc foo.c && ./a
foo.c: In function ‘main’:
foo.c:6:26: error: lvalue required as left operand of assignment
  ( ((a>b) & 1) ? a : b ) = 42;
                          ^
$

In C++:

$ g++ foo.cpp && ./a
5 42
$

If you can't use C++, I'd use @chux's idea, quoted here for posterity: #define Z(k) (((k) & 1) ? &tmp : &out) ... *Z(it_cnt+1) = m_mlt2(A, *Z(it_cnt));.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.