سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top