1. How do you access the memory address of
a variable?
Answer:
Apply the address
operator & to the variable.
e.g: &x.
2. How do you access the contents of the memory location whose address is
stored in a pointer variable?
Answer:
Apply the deference
operator * to the variable *p
3. Explain the difference between the following two declarations:
i.
int n1=n;
Answer:
The
declaration int n1=n; defines
n1 to be a clone of n; it is a separate
object that has the same value as n.
ii.
int& n2=n;
Answer:
The
declaration int& n2=n; defines n2 to be a synonym of n; it is the same
object as n, with the same address.
4. Explain the difference between the following two uses of the reference
operator &:
i.
int& r = n;
Answer:
Declares
r to be a reference for the int variable
n.
ii.
p = &n;
Answer:
Assigns the address of n to the pointer p.
5.
Explain the difference between the
following two uses of the indirection operator *:
i.
int* q = p;
Answer:
Declares q to be a pointer (memory adress) pointing to the same int to which n points.
ii.
n = *p;
Answer:
Assigns to n the int to which p points.
6.
True or false? Explain:
i. if (x == y) then (&x == &y)
i. if (x == y) then (&x == &y)
Answer:
True:
because &x
and &y are synonyms for x and y, respectively; so if (x = = y) then they all have the same value.
iii.
if (x == y) then (x*
== *y)
Answer:
False: different objects can have the same value, but different objects have different addresses.
7.
What is wrong with the following code:
int& r = 22;
Answer:
You cannot have a reference to a
constant; it’s address is not accessible.
8.
What is wrong with the following code:
int* p = &44;
Answer:
The reference operator & cannot be
applied to a constant.
9.
What is wrong with the following code:
char c = ‘w’ ;
char* p = c;