Consider the following declaration and the statements (a) to (d) written in C. Identify the statement which are correct and the ones which has an error, by giving reason. If a statement has an error correct it.
int c = 1, *pc; // declaration
(a) pc = c;
(b) *pc = &c;
(c) pc = &c;
(d) *pc = c;
(a) has an error. pc is a pointer so it should point to some memory. Correct expression is
pc = &c;
(b) is incorrect. *pc is dereference, that is the value which hold by the address pc, while &c is an address of variable c. Correct expression is
pc = &c;
(c) pc = &c; // is a correct expression
(d) *pc = c; // is a correct expression, provided that before pc was correctly initialized (as in (c))
Comments
Leave a comment