-4

I am trying this peice of code :

for (i = 0; i <= 100; i *= (i = ++sqrt((double)i)))
  printf ("%d ", i);

and I get the following error :

tp.c:10:33: error: lvalue required as increment operand
for (i = 0; i <= 100; i *= (i = ++sqrt((double)i)))

It works fine with this :

for (i = 0; i <= 100; i *= (i = sqrt((double)i) + 1))
      printf ("%d ", i);

I am not sure why is this so? My guess is that ++ operator needs a variable to operate upon, it cannot operate on a result returned by a function. Is there a way to make the code work using ++ operator?

Denil Vira
  • 25
  • 8
  • 1
    The compiler is telling you why - you need an lvalue. What you seem to be asking for makes no sense whatsoever:( – Martin James Oct 28 '15 at 23:42
  • Compile errors and undefined behavior aside the bigger issue I see here is that this is a way of writing horrendously unclear code. Don't use a `++` here, it destroys the readability of your code and readability matters a lot. – shuttle87 Oct 29 '15 at 15:21

1 Answers1

3

Indeed; ++ requires a variable or something else that is legal to modify (you're trying to modify a return value of primitive type, which is not legal) - a so-called lvalue. Why not simply use sqrt((double)i) + 1?

However, there's another problem in your code, even with this correction: i *= (i = sqrt((double)i) + 1) invokes undefined behavior because it modifies i twice. Hence, your code might potentially do anything from giving you the result you want, to giving random results, to erasing your hard drive. What exactly are you trying to achieve by your calculation?

Community
  • 1
  • 1
Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
  • Okay, this code does nothing productive, It prints squares of first 11 whole numbers. I am still learning C and I came across this problems as part of programming exercise where in it was asked to do it with using just 1 variable and 1 for loop. I apologize for my ignorance. – Denil Vira Oct 28 '15 at 23:52
  • Also, I wasn't aware until now that double modification is not a good idea. I still don't know why this is bad. I shall read up on that on my own. Thanks though for the productive reply. – Denil Vira Oct 29 '15 at 00:05
  • No need to apologize; we were all starters once. @MartinJames: Please take that into account before making such comments; I know my first lines of code weren't pretty - care to show yours? – Aasmund Eldhuset Oct 29 '15 at 00:14
  • It's just one of the rules of the language (which is there for good, but quite technical reasons) - for an explanation, see the link I added. – Aasmund Eldhuset Oct 29 '15 at 00:17
  • A much cleaner way to achieve what you want to do is to simply loop over the first 11 whole numbers and compute the square inside the loop (rather than using the square to find the original number again). – Aasmund Eldhuset Oct 29 '15 at 00:19
  • I forgot to mention earlier, the problem specification did not allow any modifications of i in the body of loop, just 1 variable and 1 for loop. I could of course just print i*i while going from 0 to 10, but the aim here was to master the for loop and not rely on the body of it. – Denil Vira Oct 29 '15 at 02:00
  • `i * i` does not modify `i`, so I'm pretty sure that this is within the rules of the task. If the task wants you to make an extremely convoluted loop header for the sake of demonstrating that it is possible, sure, but for later use: keeping code simple is much better than keeping it compact and/or clever. I would also argue that "mastering" for loops would consist of the ability to figure out the simplest way to make the loop header and loop body work together to achieve your desired result. – Aasmund Eldhuset Oct 29 '15 at 02:14