Skip to main content

Pointers

Exercises

Question 11.1

If i is a variable and p points to i, which of the following expressions are aliases for i?

  1. *p
  2. &p
  1. *&p
  2. &*p
  1. *i
  2. &i
  1. *&i
  2. &*i
  1. *p → since p points to i, and * is an indirection operator, *p is an alias for i.
  2. &pp is a integer pointer variable, and & is an address operator, so &p gives the address of p.
  3. *&p&p gives the address of p first, * then gives the value stored at that address. Since p stores the address of the object it points to, this expression gives the address of i (the object pointed to by p)
  4. &*p*p first gives the value of the object it is pointing to - the value stored in i - and the & operator gives the address of the value, which is the address of i.
  5. *i → is not a valid expression as i is not a pointer variable. So this expression is illegal. The compiler treats the * as a multiplication operator instead of the indirection operator.
  6. &i → provides the address of the variable i.
  7. *&i → is an alias of i. Since &i gives the address of the variable i, and the * operator acts as the indirection operator, it essentially gives the value stored in i.
  8. &*i → is not a valid expression as i is not a pointer variable. So this expression is illegal too. The compiler first checks the * operator first and - i not being a pointer variable - treats the * as the multiplication operator. The & operator is not even reached.

Question 11.2

If i is an int variable and p and q are pointers to int, which of the following assignments are legal?

  1. p = i;
  2. *p = &i;
  3. &p = q;
  1. p = &q;
  2. p = *&q;
  3. p = q;
  1. p = *q;
  2. *p = q;
  3. *p = *q;
  1. p = i; → this expression is legal (but wrong) as the expression tries to store the value of i into the pointer variable p.
  2. *p = &i; → this expression is legal if it is a declaration, i.e. int *p = &i. But it is wrong if it is an statement in itself (see #27 of Chapter's notes). The expression tries to store the address of i into the object that p points to, not to p itself.
  3. &p = q; → this expression is illegal as &p is not an lvalue - an object stored in memory - but rather a memory address.
  4. p = &q; → this expression is illegal as q itself is a pointer variable. To further understand this, we need to understand the &i is of type (int *), which is the address of a variable that points to an integer, while &p and &q is of type (int **), which is the address of a variable that points to a pointer of type int.
  5. p = *&q; → the expression can be broken into sub-expressions. First &q gives the address of the variable that points to a pointer. The * operator gives the object that is stored in the address, which essentially is the same as saying that assign the integer pointer that is already assigned to q into the integer pointer variable p.
  6. p = q; → this expression assigns the pointer to the object stored in q to the pointer variable p.
  7. p = *q;*q is gives the object pointed to by the address which is stored in q. This expression does not store the pointer to an integer variable as *q results in an integer value itself, while p expects an pointer to an integer variable.
  8. *p = q;*p expects an integer variable, (or an integer value, but is discouraged as using p only can lead to undefined behavior). But since q holds the pointer to an object that stores integer, it does not work as intended.
  9. *p = *q; → this expression stores the object that is pointed to by q into the object that is pointed to by p.

Question 11.3

The following function supposedly computes the sum and average of the numbers in the array a, which has length n. avg and sum point to variables that the function should modify. Unfortunately, the function contains several errors; find and correct them.

void avg_sum(double a[], int n, double *avg, double *sum)
{
int i;
sum = 0.0;
for (i = 0; i < n; i++)
sum += a[i];
avg = sum / n;
}

Enter 5 numbers: 1 2 3 4 5 The average of the numbers is: 3.00 The sum of the numbers is: 15.00


Question 11.4

Write the following function:

void swap(int *p, int *q);

When passed the addresses of two variables, swap should exchange the values of the variables:

swap(&i, &j); /* exchanges values of i and j */

Enter two numbers: 1 2 The swapped numbers are: 2 and 1


Question 11.5

Write the following function:

void split_time(long total_sec, int *hr, int *min, int *sec);

total_sec is a time represented as the number of seconds since midnight. hr, min, and sec are pointers to variables in which the function will store the equivalent time in hours (0-23), minutes (0-59). and seconds (0-59), respectively.

Enter a time in seconds since midnight: 45 The time in hr:min:sec is: 0: 0:45


Question 11.6

Write the following function:

void find_two_largest(int a[], int n, int *largest,
int *second_largest);

When passed an array a of length n, the function will search a for its largest and second largest elements, storing them in the variables pointed to by largest and second_largest, respectively.

Enter 10 elements: 1 2 3 4 5 6 7 8 9 10 The largest element in the array is: 10 The second largest element in the array is: 9


Question 11.7

Write the following function:

void split_date(int day_of_year, int year,
int *month, int *day);

day_of_year is an integer between 1 and 366, specifying a particular day within the year designated by year, month and day point to variables in which the function will store the equivalent month (1-12) and day within that month (1-31).

Enter the date (1-366): 23 Enter the year: 1994 In yy/mm/dd format, the date is: 1994/01/23


Question 11.8

Write the following function:

int *find_largest(int a[], int n);

When passed an array a of length n, the function will return a pointer to the array's largest element.

Enter 10 numbers: 1 2 3 4 5 6 7 8 9 10 The largest element in the array is: 10

Programming Projects

Project 11.1

Modify Programming Project 7 from Chapter 2 so that it includes the following function:

void pay_amount(int dollars, int *twenties, int *tens,
int *fives, int *ones);

The function determines the smallest number of $20, $10, $5, and $1 bills necessary to pay the amount represented by the dollars parameter. The twenties parameter points to a variable in which the function will store the number of $20 bills required. The tens, fives, and ones parameters are similar.

#include <stdio.h>

void pay_amount (int dollars, int *twenties, int *tens, int *fives, int *ones);

int main (void) {

int dollars, twenties_bill, tens_bill, fives_bill, ones_bill;

printf("Enter the amount in dollar: ");

Project 11.2

Modify Programming Project 8 from Chapter 5 so that it includes the following function:

void find_closest_flight(int desired_time,
int *departure_time,
int *arrival_time);

This function will find the flight whose departure time is closest to desired_time (expressed in minutes since midnight). It will store the departure and arrival times of this flight (also expressed in minutes since midnight) in the variables pointed to by departure_time and arrival_time, respectively.

#include <stdio.h>
#include <stdbool.h>

void find_closest_flight (int desired_time, int *departure_time, int *arrival_time);

int main (void) {

int user_time = 0;
int hour, min;

Project 11.3

Modify Programming Project 3 from Chapter 6 so that it includes the following function:

void reduce(int numerator, int denominator,
int *reduced_numerator,
int *reduced_denominator);

numerator and denominator are the numerator and denominator of a fraction. reduced_numerator and reduced_denominator are pointers to variables in which the function will store the numerator and denominator of the fraction once it has been reduced to lowest terms.

#include <stdio.h>

void reduce (int numerator, int denominator, int *reduced_numerator, int *reduced_denominator);

int main (void) {

int num, denom;
int red_num, red_denom;

Project 11.4

Modify the poker.c program of Section 10.5 by moving all external variables into main and modifying functions so that they communicate by passing arguments. The analyze_hand function needs to change the straight, flush, four, three, and pairs variables, so it will have to be passed pointers to those variables.

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

#define NUM_RANKS 13
#define NUM_SUITS 4
#define NUM_CARDS 5

void read_cards (int num_in_rank[], int num_in_suit[]);