ANSWERS TO EXERCISES ON POINTERS
1. None. Some people prefer A to indicate that *x is an int;
thus x is a pointer to an int. Others prefer B to indicate that
x is the variable being declared. Not many people use C; it was
just included to demonstrate that the position of the * is
irrelevant.
2. x = 5, y = 10. The function has no effect since integers are
passed by value. Variables a and b contain only local copies
of the integers 5 and 10.
3. x = 10, y = 5. Since the memory addresses of x and y are passed,
the function is able to correctly swap their values.
4. x = {0, 4, 2, 3, 1}. The memory address of the 0th array
element of a is passed to the function.
5. x = {0, 4, 2, 3, 1}. Address arithmetic says that a+1 is the
memory address of the 1st array element and a+4 is a pointer to
the 4th.
6. 0 1 2 Same as print3(a).
2 3 4 Same as print3(a+2). Just like array starts at element 2.
4 ? ? Out of bounds access.
7. Efficiency. Arrays can have billions of elements. The overhead
of copying them each time you call a function would be overwhelming.
It also means that you can pass a "sub-array" to a function for
processing as in the previous question.
8. None. It is a matter of style. Beginners usually prefer the first.
9. scanf() needs to change the value of n. A function cannot change the
value of an integer unless it knows its memory address. Instead
of passing the current value of n, we use &n to pass its memory address.