Multiple choice

Given the following definition: int i=10; int *p; Which of the following assignments is invalid?

  1. i=*p

  2. *p=i

  3. p=&i

  4. *p=&i

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Assignment D (*p=&i) is invalid. The left side *p dereferences the pointer to access the integer value it points to, while &i gives the address of i. You cannot assign an address to an integer value. Assignments A and B require p to point to valid memory first, and C correctly stores i's address in p.