Solution to Homework #1
1.1 Name and illustrate 5 operations that can be applied to pointers.
+,-,*,/ : arithmatic : p + q
* : dereferencing operator : *ptr
++ : increment or decrement : ++p
== : equality : p == q
= : assignment : p = &a
1.2 Consider
int A, B;
int *Ptr;
int **PtrPtr;
Ptr = &A;
PtrPtr = &Ptr;
a. Is this legal?
:YES
b. What are the values of *Ptr and **PtrPtr?
:Both have the value of A.
c. Using no other objects besides those declared above, how can we alter
PtrPtr so that it points at a pointer to B without directly touching Ptr?
: *PtrPtr = &B
d. Is the following legal? PtrPtr = Ptr
: NO, it is the type-mismatch.
1.3. Answer the following:
a. Is *&X always equal to X? If not, give an example.
:YES
Explanation: because *&X gives the value of X not matter what type X is
declared to be, and the address of X is always defined, so &X can
have the derefence operator * applied.
b. Is &*X always equal to X? If not, given an example.
:NO, if X is defined as INT, then *X is not defined.
1.4 For the declarations
int A = 5;
int *Ptr = &A;
What are the values of the following?
a. Ptr
: &A
b. *Ptr
: 5
c. Ptr == A
: Illegal, type-mismatch.
d. Ptr == &A
: TRUE
e. &Ptr
: address of Ptr
f. *A
: error
g. *&A
: 5
h. **&Ptr
: 5
1.5 Give the types of all the identifiers declared here and the types of the
expressions. Is any expression illegal?
a. struct S {
int A;
S *B;
};
: S is a struct type consisting of two member; A of type integer, and
B of type pointer to struct S.
b. S Z;
: Z is declared as a struct of type struct S.
c. S *X;
: X is declared as a pointer to struct S.
d. S Y[10];
: Y is an array of size 10 of struct S.
e. S *U[10]
: U is an array of size 10 of pointer to type struct S.
f. X->A
: value of int member of struct X. A is integer
g. X->B
: member B of struct X, and B is a pointer to type struct S
h. Z.B
: member B of Z of type struct S. B is a pointer to struct S.
i. Z.A
: value of member A of Z of type struct S. A is integer
j. *Z.A
: Illegal expression. Z.A is not a pointer
k. (*Z).A
: Illegal expression. Z is not a pointer.
l. X->B-Z.B
: pointer arithmatic
m. Y->A
: integer member of first struct in array Y.
n. Y[1]
: the second element of array Y.
o. Y[1].A
: integer member of the second element of array Y.
p. Y[1].B
: pointer member of the second element of array Y.
q. U[2]
: pointer to struct S
r. *U[2]
: dereferencing a pointer to struct S
s. U[2]->A
: integer member of a struct of type struct S which is pointed to
by U[2]
t. U[2]->B
: pointer member of a struct of type struct S which is pointed to
by U[2]
u. U[10]
: pointer to a struct of type struct S. The compiler will compile
this even though it is out of range.
v. &Z
: address of Z.
w. &X
: address of X.
x. U
: pointer to the first element of array U.
y. Y
: pointer to the first element of array Y.
1.6 Draw a picture that illustrates the results after each of the following
statements, which are executed sequentially.
int A = 3; (1)
int& B = A; (2)
int& C = B; (3)
B = 5; (4)
C = 2; (5)
: After (1), A = 3
After (2), B = A = 3
After (3), C = B = A = 3
After (4), C = B = A = 5
After (5), C = B = A = 2
1.7 Do you think the following is legal? Why or why not?
int A = 3;
const int& B = A;
: It is legal, however, it is considered a bad programming because
B is declared constant but if the value of A is changed, B's value
is also changed.
1.8 What is wrong with omitting spacing in the following
*X/*Y
: If there is no space between / and *, then Y will be ignored because
compiler consider /* as the start of the comment.