Consider the following declarations in C: int C, *PTR; Which of the following…
2018
Consider the following declarations in C:
int C, *PTR;
Which of the following statements is TRUE?
- A.
PTR = C;
- B.
*PTR = &C;
- C.
PTR = &C;
- D.
C = &PTR;
Attempted by 218 students.
Show answer & explanation
Correct answer: C
Concept: In C, a pointer variable stores the memory address of a value of its base type. An assignment is valid only when both sides refer to matching types — an address must be assigned to a pointer, and a plain value to a variable of that same base type.
Application: In the declaration int C, *PTR;, only PTR carries the *, so C is a plain int and PTR is a pointer to int. To validly assign to PTR, the right-hand side must be the address of an int. The expression &C is exactly that address, so PTR = &C; matches PTR's declared type on both sides.
Cross-check against the rule, the other statements each break it:
PTR = C; assigns the plain integer value of C to the pointer PTR — a value where an address is required.
*PTR = &C; dereferences PTR to an int lvalue and then tries to store an address (&C) into it — an address where a plain value is required, and PTR has not even been initialised to point anywhere valid.
C = &PTR; assigns the address of the pointer PTR into the plain integer C — again an address where a value is required.
Only PTR = &C; assigns an address to a pointer and matches the declared types on both sides, so it is the valid (TRUE) statement.