Machine-translated by Google. Questions are authored in English.
Top

Discussion

Consider the following 'C' program fragment :

int main( )
{
char *a, *b, c[10], d[10];
a=b;
b=c;
c=d;
d=a;
return φ;
}

Choose the statement having error from the following :

  • A.b=c; and c=d;
  • B.a=b; and b=c;
  • C.c=d; and d=a;
  • D.a=b; and d=a;

Answer: C

The statements with errors are c=d; and d=a;.

In C, an array name such as c or d is not a modifiable lvalue: it stands for the fixed address of the array, so an array cannot be assigned to. Both c=d (array to array) and d=a (pointer to array) are therefore compilation errors.

a=b assigns one char pointer to another, and b=c assigns the address of c's first element to a pointer (the array name decays to a pointer), so both are legal. Every option that names a=b or b=c as an error is wrong.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.