Pointers
Exercises
Question 11.1
If i is a variable and p points to i, which of the following expressions are aliases for i?
|
|
|
|
- Answer
*p→ sinceppoints toi, and*is an indirection operator,*pis an alias fori.&p→pis a integer pointer variable, and&is an address operator, so&pgives the address ofp.*&p→&pgives the address ofpfirst,*then gives the value stored at that address. Sincepstores the address of the object it points to, this expression gives the address ofi(the object pointed to byp)&*p→*pfirst gives the value of the object it is pointing to - the value stored ini- and the&operator gives the address of the value, which is the address ofi.*i→ is not a valid expression asiis not a pointer variable. So this expression is illegal. The compiler treats the*as a multiplication operator instead of the indirection operator.&i→ provides the address of the variablei.*&i→ is an alias ofi. Since&igives the address of the variablei, and the*operator acts as the indirection operator, it essentially gives the value stored ini.&*i→ is not a valid expression asiis not a pointer variable. So this expression is illegal too. The compiler first checks the*operator first and -inot 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?
|
|
|
- Answer
p = i;→ this expression is legal (but wrong) as the expression tries to store the value ofiinto the pointer variablep.*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 ofiinto the object thatppoints to, not topitself.&p = q;→ this expression is illegal as&pis not an lvalue - an object stored in memory - but rather a memory address.p = &q;→ this expression is illegal asqitself is a pointer variable. To further understand this, we need to understand the&iis of type(int *), which is the address of a variable that points to an integer, while&pand&qis of type(int **), which is the address of a variable that points to a pointer of typeint.p = *&q;→ the expression can be broken into sub-expressions. First&qgives 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 toqinto the integer pointer variablep.p = q;→ this expression assigns the pointer to the object stored inqto the pointer variablep.p = *q;→*qis gives the object pointed to by the address which is stored inq. This expression does not store the pointer to an integer variable as*qresults in an integer value itself, whilepexpects an pointer to an integer variable.*p = q;→*pexpects an integer variable, (or an integer value, but is discouraged as usingponly can lead to undefined behavior). But sinceqholds the pointer to an object that stores integer, it does not work as intended.*p = *q;→ this expression stores the object that is pointed to byqinto the object that is pointed to byp.
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;
}
- Output
- Program
Enter 5 numbers: 1 2 3 4 5 The average of the numbers is: 3.00 The sum of the numbers is: 15.00
#include <stdio.h>
#define N 5
void avg_sum (int a[], int n, double *avg, double *sum);
int main (void) {
int arr[N] = {0};
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 */
- Output
- Program
Enter two numbers: 1 2 The swapped numbers are: 2 and 1
#include <stdio.h>
void swap (int *p, int *q);
int main (void) {
int i, j;
printf("Enter two numbers: ");
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.
- Output
- Program
Enter a time in seconds since midnight: 45 The time in hr:min:sec is: 0: 0:45
#include <stdio.h>
void split_time (long total_sec, int *hr, int *min, int *sec);
int main (void) {
long total_time;
int hour, minute, second;
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.
- Output
- Program
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
#include <stdio.h>
#define N 10
void find_two_largest (int a[], int n, int *largest, int *second_largest);
int main (void) {
int arr[N] = {0};
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).
- Output
- Program
Enter the date (1-366): 23 Enter the year: 1994 In yy/mm/dd format, the date is: 1994/01/23
#include <stdio.h>
#include <stdbool.h>
void split_date (int day_of_year, int year, int *month, int *day);
int main (void) {
int total_day, year;
int month, day;
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.
- Output
- Program
Enter 10 numbers: 1 2 3 4 5 6 7 8 9 10 The largest element in the array is: 10
#include <stdio.h>
#define N 10
int *find_largest (int a[], int n);
int main (void) {
int arr[N] = {0};
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.
- Program
#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.
- Program
#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.
- Program
#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.
- Program
#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[]);