-6

What is the meaning of the following line in C. What is the order of execute it?

float *x,*y;
*x++=*y++

Can any one explain how this evaluated?

Afnan
  • 35
  • 1
  • 1
  • 5

2 Answers2

10

For the original code:

x++ = y++

This line will never be evaluated because it is not legal C and will not be compiled. The result of x++ is not an lvalue and is not permitted on the left side of an assignment.

For the updated code:

float x,y;
*x++ = *y++

This is not legal because * cannot be applied to a float.

I will add this code:

float *x, *y;
*x++ = *y++;

This code says:

  • Let a temporary, say float *xt, equal x.
  • Let a temporary, say float *yt, equal y.
  • Add one to x.
  • Add one to y.
  • Assign *xt = *yt.

The actual operations may be performed in various orders, provide xt takes its value before x is updated, yt takes its value before y is updated, and xt and yt are defined before they are used.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

If the two variables are int (or another simple type), the original statement (x++=y++) is illegal. However, in the case of pointer arithmetic, this is legal. One way to copy a string where both x and y are char *, is

while ( *x++ = *y++ );

in this case, the pointer is incremented after the corresponding character is copied and the loop iterates until it encounters the end of string character (a NULL) in the string pointed to by y.

unxnut
  • 8,509
  • 3
  • 27
  • 41
  • Darn, some downvote-happy dude is here. I think I know who that is, too. Anyway, there's nothing wrong with this answer, so +1. – Sergey Kalinichenko May 10 '13 at 19:40
  • Thanks, I was wondering about it myself. Anyway, I see that the question has been changed as well (though it is still wrong). – unxnut May 10 '13 at 19:41
  • @dasblinkenlight: Can you show a declaration for `x` and `y` such that `x++ = y++` compiles? If not, then the statement that “this is legal”, even qualified with “in the case of pointer arithmetic”, is not true. A statement that “a modified statement, such as shown below, is legal” could be true. The down vote is not permanent; if the answer is edited, I will reconsider it. – Eric Postpischil May 10 '13 at 19:42
  • Wait, the answer clarified that it should be with the pointer arithmetic and the example considered is that of a string. It does not do x++ = y++ and so, is correct. – unxnut May 10 '13 at 19:46
  • @EricPostpischil Not only does the answer make it abundantly clear that "this is legal" is referring to a case with pointers, it also supplies an example. – Sergey Kalinichenko May 10 '13 at 19:46
  • @dasblinkenlight: Even if `x` and `y` are pointers, such as `float *x, *y;`, `x++ = y++` is not legal. My judgement is that the use (or misuse) of the pronoun “this” is misleading and makes this answer not useful. – Eric Postpischil May 10 '13 at 19:54
  • @EricPostpischil You seem to misunderstand the meaning of "ad hominem": my comment has been about your comment, and only about your comment. Honestly, the comments that depend on attribution of the pronoun "this" are inevitably of hair-splitting nature, which is what I tried to point out. My deleted comment has not in any way meant to disrespect or offend you personally, so I regret it if you happened to construe my comment as offensive. – Sergey Kalinichenko May 11 '13 at 01:30