Pointers and Arrays
Exercises
Question 12.1
Suppose that the following declarations are in effect:
int a[] = {5, 15, 34, 54, 14, 2, 52, 72};
int *p = &a[1], *q = &a[5];
- What is the value of
*(p + 3)? - What is the value of
*(q - 3)? - What is the value of
q - p? - Is the condition
p < qtrue or false? - Is the condition
*p < *qtrue or false?
- Answer
Since p is a pointer that points to the element 1 of the array a, and q points to the element 5 of the array a. The following effects are obsevered when:
*(p + 3)→ppoints to the element 1 of the array, adding 3 will make it point to the(1 + 3)→4, i.e. element 4 of the array. Since the indirection operator (*) is in use, it will result in fetching the element 4 of the array.*(q - 3)→qpoints to the element 5 of the array, subtracting 3 from q implies that q now points to(5 - 3)→2, i.e. element 2 of the arraya. Use of the indirection operator make it fetch the data/object stored in the locationqis pointing to - the element 2.q - p→qstores the address of the element 5 of the arraya, andpstores the address for element 1 of the arraya. Say that the array has the address as [1000, 1004, 1008, 1012, 1016, 1020, 1024, 1028] (memory address are not stored as decimal, but rather as hex, but it is used for representation purpose. Also, it is assumed thatinttakes 4 bytes.), and sincepstores the value 1004 - the address of element 1 in the array - whileqstores the value 1020, the result would be1020 - 1004→16 / 4→4.- The condition
p < qis true sincepis indeed lesser compared toq. Since the relative position ofpis less as compared to the position ofq, the inequality operator holds true. - The condition
*p < *qis false as*pgives the value 15 (the element 1 of arrayathatpis pointing to) and*qgives the value 2 (the element 5 of arrayathatqis pointing to). As 15 is not less than 2, the result of the operation would be 0 (false).
Question 12.2
Suppose that high, low, and middle are all pointer variables of the same type, and that low and high point to elements of an array. Why is the following statement illegal, and how could it be fixed?
middle = (low + high) / 2;
- Answer
The following operation is illegal as addition of two pointer variables are not valid operation (see #3 and #6 of Chapter's notes) (also it is an invalid operation to add two (int *) variables). To fix this issue, we need to do the following:
middle = &arr[n / 2];
where n is the length of the array, and arr is the array in question. &arr[n/2] gives the middle element of the array and the & (address) operator provides the address of the respective element in the array.
Question 12.3
What will be the contents of the a array after the following statements are executed?
#define N 10
int a[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *p = &a[0], *q = &a[N-1], temp;
while (p < q) {
temp = *p;
*p++ = *q;
*q-- = temp;
}
- Answer
The following data is stored in respective variables:
a→ stores N (10) elements and is an arrayp→ stores the pointer to the element 0 of the arrayaq→ stores the pointer to the element N - 1 (element 9) of the arraya.
The execution of the while loop is as follows:
- First iteration (
p < q) istrueas the relative position ofpis less as compared to the relative position ofq.tempstores the object being pointed by the pointer variablep→temp= 1*p++happens like this →*(p++)→pis incremented later, but the value atp's current pointing location is updated to the one stored in*q→*p=*q→*&a[0]= 10p = p + 1→p = &a[1]*q--→*(q--)→qis decremented later, but first,*qis assigned the value stored intemp. This means*q = 1→*&a[9] = 1q = q - 1→q = &a[8]
- Second iteration (
p < q) istrueasp(&a[1]) <q(&a[8])temp = *p→temp = *&a[1]→temp = 2*p++ = *q→*(p++) = *&a[8]→*&a[1] = 9p = p + 1→p = &a[2]*q-- = temp→*(q--) = 2→*&a[8] = 2q = q - 1→q = &a[7]
- Third iteration (
p < q) istrueasp(&a[2]) <q(&a[7])temp = *p→temp = *&a[2]→temp = 3*p++ = *q→*(p++) = *&a[7]→*&a[2] = 8p = p + 1→p = &a[3]*q-- = temp→*(q--) = 3→*&a[7] = 3q = q - 1→q = &a[6]
- Fourth iteration (
p < q) istrueasp(&a[3]) <q(&a[6])temp = *p→temp = *&a[3]→temp = 4*p++ = *q→*(p++) = *&a[6]→*&a[3] = 7p = p + 1→p = &a[4]*q-- = temp→*(q--) = 4→*&a[6] = 4q = q - 1→q = &a[5]
- Fifth iteration (
p < q) istrueasp(&a[4]) <q(&a[5])temp = *p→temp = *&a[4]→temp = 5*p++ = *q→*(p++) = *&a[5]→*&a[4] = 6p = p + 1→p = &a[5]*q-- = temp→*(q--) = 5→*&a[5] = 5q = q - 1→q = &a[4]
- Sixth iteration (
p < q) isfalseasp(&a[5]) <q(&a[4]) isfalse. So the program will break out of the loop.
The resulting values in the array are: a = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
Question 12.4
Rewrite the make_empty, is_empty, and is_full functions of Section 10.2 to use the pointer variable top_ptr instead of the integer variable top.
- Program
- Output
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define STACK_SIZE 100
// external/global variable declaration and initialization.
int contents[STACK_SIZE] = {0};
int *top_ptr = &contents[0];
This Program generates a stack of size 100 Here are the avaiable commands for the stack: 1. Push into the stack. 2. Pop from the stack. 3. Current stack position. 4. Clear the stack. 5. Exit. Enter your option: 1 Enter the item to push: 1 [SUCCESS] Item pushed into stack. Enter your option: 1 Enter the item to push: 2 [SUCCESS] Item pushed into stack. Enter your option: 1 Enter the item to push: 3 [SUCCESS] Item pushed into stack. Enter your option: 3 [LOG] The current top position is: 3. Enter your option: 2 [LOG] The popped item is: 3. Enter your option: 3 [LOG] The current top position is: 2. Enter your option: 4 [LOG] Clearing the stack. [SUCCESS] Cleared the stack. Enter your option: 3 [LOG] The current top position is: 0. Enter your option: 5
Question 12.5
Suppose that a is a one-dimensional array and p is a pointer variable. Assuming that the assignment p = a has just been performed, which of the following expressions are illegal because of mismatched types? Of the remaining expressions, which are true (have a nonzero value)?
p == a[0]p == &a[0]*p == a[0]p[0] == a[0]
- Answer
Since the assignment p = a has been performed. The following things happen:
p == a[0]will befalseaspholds the pointer to the element 0 of the arraya, buta[0]gives the data stored in the element 0 of the array. This expression can be roughly translated to&a[0] == a[0], which isfalse.p == &a[0]will betrueaspholds the pointer to the element 0 of the arrayawhile&a[0]also gives the pointer to the element 0 of the arraya.*p == a[0]holds true aspholds the pointer to the element 0, and*pgives the data that the pointer points to.p[0] == a[0]holds true as the compiler interpretsp[0]as*(p + 0), wherepstores the pointer to the element 0 of the arraya.
Question 12.6
Rewrite the following function to use pointer arithmetic instead of array subscripting. (In other words, eliminate the variable i and all uses of the [] operator.) Make as few changes as possible.
int sum_array(const int a[], int n)
{
int i, sum;
sum = 0;
for(i = 0; i < n; i++)
sum += a[i];
return sum;
}
- Program
- Output
#include <stdio.h>
#define N 10
int sum_array (const int *a, int n);
int main (void) {
int arr[N] = {0};
Enter the elements: 1 2 3 4 5 6 7 8 9 10 The sum of the elements is: 55
Question 12.7
Write the following function:
bool search(const int a[], int n, int key);
a is an array to be searched, n is the number of elements in the array, and key is the search key. search should return true if key matches some element of a, and false if it doesn't. Use pointer arithmetic - not subscripting - to visit array elements.
- Program
- Output
#include <stdio.h>
#include <stdbool.h>
#define N 10
bool search (const int *a, int n, int key);
int main (void) {
Enter 10 elements: 1 2 3 4 5 6 7 8 9 10 Enter the value to search in array: 4 The value is in the array.
Question 12.8
Rewrite the following function to use pointer arithmetic instead of array subscripting. (In other words, eliminate the variable i and all uses of the [] operator.) Make as few changes as possible.
void store_zeros(int a[], int n)
{
int i;
for(i = 0; i < n; i++)
a[i] = 0;
}
- Program
- Output
#include <stdio.h>
#define N 10
void store_zeros (int *a, int n);
int main (void) {
int arr[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
After clearing the array, the elements are: Element 0 has value: 0 Element 1 has value: 0 Element 2 has value: 0 Element 3 has value: 0 Element 4 has value: 0 Element 5 has value: 0 Element 6 has value: 0 Element 7 has value: 0 Element 8 has value: 0 Element 9 has value: 0
Question 12.9
Write the following function:
double inner_product(const double *a, const double *b,
int n);
a and b both point to arrays of length n. The function should return a[0] * b[0] + a[1] * b[1] + ... + a[n-1] * b[n-1]. Use pointer arithmetic - not subscripting - to visit array elements.
- Program
- Output
#include <stdio.h>
#define N 10
double inner_product (const double *a, const double *b, int n);
int main (void) {
double arr_1[N] = {0.0};
Enter 10 elements for the first array: 1 2 3 4 5 6 7 8 9 10 Enter 10 elements for the second array: 11 12 13 14 15 16 17 18 19 20 The sum of the product of the elements are: 935.00
Question 12.10
Modify the find_middle function of Section 11.5 so that it uses pointer arithmetic to calculate the return value.
- Program
- Output
#include <stdio.h>
#define N 10
int *find_middle (int *a, int n);
int main (void) {
int arr[10] = {0};
Enter 10 elements: 1 2 3 4 5 6 7 8 9 10 The middle element of the array is: 6
Question 12.11
Modify the find_largest function so that it uses pointer arithmetic — not subscripting — to visit array elements.
- Program
- Output
#include <stdio.h>
#define N 10
int find_largest (const int *a, int n);
int main (void) {
int arr[N] = {0};
Enter 10 elements: 1 2 3 4 5 6 7 8 9 10 The largest value in the array is: 10
Question 12.12
Write the following function:
void find_two_largest(const int *a, int n, int *largest,
int *second_largest);
a points to an array of length n. The function searches the array for its largest and second largest elements, storing them in the variables pointed to by largest and second_largest, respectively. Use pointer arithmetic — not subscripting — to visit array elements.
- Program
- Output
#include <stdio.h>
#define N 10
void find_two_largest (const int *a, int n, int *largest, int *second_largest);
int main (void) {
Enter 10 elements: 1 2 3 4 5 6 7 8 9 10 The largest element in the array is 10 and the second largest is: 9
Question 12.13
Section 8.2 had a program fragment in which two nested for loops initialized the array ident for use as an identity matrix. Rewrite this code, using a single pointer to step through the array one element at a time. Hint: Since we won't be using row and col index variables, it won't be easy to tell where to store 1. Instead, we can use the fact that the first element of the array should be 1, the next N elements should be 0. the next element should be 1, and so forth. Use a variable to keep track of how many consecutive Os have been stored: when the count reaches N, it's time to store 1.
- Program
- Output
#include <stdio.h>
#define N 5
int main (void) {
double ident[N][N];
double *ident_p;
int i = 0;
The elements in the matrix is: 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0
Question 12.14
Assume that the following array contains a week's worth of hourly temperature readings, with each row containing the readings for one day:
int temperatures[7][24];
Write a statement that uses the search function (see Exercise 7) to search the entire temperatures array for the value 32.
- Program
- Output
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define NUM_ROWS 7
#define NUM_COLS 24
bool search_temp (int *temp_ptr, int row_size, int col_size, int temp_key);
Enter a temperature to search for: 24 The temperature matrix is: 21 6 80 18 62 4 3 30 38 89 44 25 54 78 49 7 58 65 29 63 81 15 51 58 58 28 61 14 7 60 13 95 33 59 12 61 29 82 92 33 36 1 24 90 79 74 63 2 4 58 66 51 39 16 8 98 10 36 11 84 62 91 79 95 50 91 56 45 39 13 79 75 14 2 31 59 42 94 27 46 52 93 98 91 75 72 54 86 74 66 35 36 56 13 97 72 71 18 17 9 98 96 50 78 98 81 36 40 41 30 52 93 22 15 49 98 54 3 49 27 35 84 29 91 64 93 63 34 10 46 9 75 41 59 52 4 6 55 10 47 85 29 5 6 44 54 70 98 23 85 92 58 35 87 15 99 79 44 99 56 90 7 30 97 32 48 67 38 24 is recorded.
Question 12.15
Write a loop that prints all temperature readings stored in row i of the temperatures array (see Exercise 14). Use a pointer to visit each element of the row.
- Program
- Output
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_ROWS 7
#define NUM_COLS 24
void print_row (int *temp_ptr, int row_index, int col_size);
Enter the row size (1 to 7): 4 The temperature matrix is: 14 35 78 15 50 56 49 74 27 65 14 21 28 19 91 36 36 57 86 79 76 42 29 12 98 90 66 24 78 10 61 92 46 38 6 96 60 55 35 88 86 16 75 81 35 65 16 71 22 69 15 98 10 44 76 74 100 7 98 77 17 59 68 29 63 41 24 90 62 26 77 48 42 51 28 77 83 10 13 4 79 28 68 55 38 43 29 37 50 26 81 33 51 14 63 81 55 53 70 17 79 46 31 20 63 25 63 45 35 76 15 81 3 49 35 7 92 30 45 7 23 92 7 74 72 36 20 94 89 90 77 34 1 7 20 65 32 84 76 33 59 58 13 29 6 15 36 65 45 47 38 34 38 45 8 77 81 28 70 36 85 12 70 86 86 90 16 17 The temperature on row 4 is as follows: 42 51 28 77 83 10 13 4 79 28 68 55 38 43 29 37 50 26 81 33 51 14 63 81
Question 12.16
Write a loop that prints the highest temperature in the temperatures array (see Exercise 14) for each day of the week. The loop body should call the find_largest function,
passing it one row of the array at a time.
- Program
- Output
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_ROWS 7
#define NUM_COLS 24
int find_largest (int *arr_ptr, int col_size);
The temperature matrix is: 11 11 68 11 63 3 4 90 27 13 7 3 29 4 67 86 48 67 94 99 68 88 62 1 17 26 11 16 11 15 58 89 93 92 67 55 96 71 10 89 50 17 92 46 88 24 31 35 91 91 34 25 44 62 93 62 55 3 78 66 85 1 54 77 59 87 31 54 57 42 43 7 25 0 53 13 25 50 48 82 6 48 6 50 77 99 78 31 69 21 63 53 22 83 30 82 70 27 1 26 35 10 100 61 11 18 74 2 68 88 84 74 2 57 91 45 21 68 76 90 90 38 9 78 87 5 25 56 32 27 83 34 3 48 61 14 67 34 16 0 88 67 41 56 23 31 0 44 65 42 0 20 46 9 99 33 81 23 89 79 16 37 12 20 86 73 0 18 The highest temperature in day 1 is: 99 The highest temperature in day 2 is: 96 The highest temperature in day 3 is: 93 The highest temperature in day 4 is: 99 The highest temperature in day 5 is: 100 The highest temperature in day 6 is: 90 The highest temperature in day 7 is: 99
Question 12.17
Rewrite the following function to use pointer arithmetic instead of array subscripting. (In other words, eliminate the variables i and j and all uses of the [] operator.) Use a single loop instead of nested loops.
int sum_two_dimensional_array(const int a[][LEN], int n)
{
int i, j, sum = 0;
for (i = 0; i < n; i++)
for(j = 0; j < LEN; j++)
sum += a[i][j];
return sum;
}
- Program
- Output
#include <stdio.h>
#define NUM_ROWS 5
#define NUM_COLS 6
int sum_two_dimensional_array (const int *arr_ptr, int row_size, int col_size);
int main (void) {
Enter 6 elements for row 1: 1 2 3 4 5 6 Enter 6 elements for row 2: 7 8 9 10 11 12 Enter 6 elements for row 3: 13 14 15 16 17 18 Enter 6 elements for row 4: 19 20 21 22 23 24 Enter 6 elements for row 5: 25 26 27 28 29 30 The sum of all the elements are: 465
Question 12.18
Write the evaluate_position function described in Exercise 13 of Chapter 9. Use pointer arithmetic — not subscripting — to visit array elements. Use a single loop instead of nested loops.
- Program
- Output
#include <stdio.h>
#define LEN 8
int evaluate_position (const char *board);
int main (void) {
char board[LEN][LEN] = {{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'},
The given configuration is: R N B Q K B N R P P P P P P P P . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . p p p p p p p p r n b q k b n r
The board evaluation is: 0
Programming Projects
Project 12.1
(a) Write a program that reads a message, then prints the reversal of the message:
Enter a message: Don't get mad, get even.
Reversal is: .neve teg ,dam teg t'noD
Hint: Read the message one character at a time(using getchar) and store the characters in an array. Stop reading when the array is full or the character read is '\n'.
(b) Revise the program to use a pointer instead of an integer to keep track of the current position in the array.
- Program (a)
- Program (b)
#include <stdio.h>
#define MSG_SIZE 50
int main (void) {
char message[MSG_SIZE];
char *msg_ptr = message;
int message_count = 0;
#include <stdio.h>
#define MSG_COUNT 10
#define MSG_SIZE 50
int main (void) {
char message[MSG_COUNT][MSG_SIZE];
// declaring a character pointer that points to an array of character
Project 12.2
(a) Write a program that reads a message, then checks whether it's a palindrome (the letters in the message are the same from left to right as from right to left):
Enter a message: He lived as a devil, eh?
Palindrome
Enter a message: Madam, I am Adam.
Not a palindrome
Ignore all characters that aren't letters. Use integer variables to keep track of positions in the array.
(b) Revise the program to use pointers instead of integers to keep track of positions in the array.
- Program
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#define MSG_SIZE 50
bool is_palindrome (char *message, int msg_size);
int main (void) {
Project 12.3
Simplify Programming Project 1(b) by taking advantage of the fact that an array name can be used as a pointer.
- Program
#include <stdio.h>
#define MSG_SIZE 50
int main (void) {
char message[MSG_SIZE];
char ch;
int message_count = 0;
Project 12.4
Simplify Programming Project 2(b) by taking advantage of the fact that an array name can be used as a pointer.
- Program
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#define MSG_SIZE 50
bool is_palindrome (const char *message, int msg_size);
int main (void) {
Project 12.5
Modify Programming Project 14 from Chapter 8 so that it uses a pointer instead of an integer to keep track of the current position in the array that contains the sentence.
- Program
#include <stdio.h>
int main (void) {
char original_sentence[50];
char *msg_ptr = original_sentence;
char terminating_char;
char ch;
int i;
Project 12.6
Modify the qsort.c program of Section 9.6 so that low, high, and middle are pointers to array elements rather than integers. The split function will need to return a pointer, not an integer.
- Program
#include <stdio.h>
#include <stdbool.h>
#define LEN 10
void quicksort (int *low, int *high);
int *split (int *low, int *high);
int main (void) {
Project 12.7
Modify the maxmin.c program of Section 11.4 so that the max_min function uses a pointer instead of an integer to keep track of the current position in the array.
- Program
#include <stdio.h>
#define N 10
void max_min (int *a, int n, int *max, int *min);
int main (void) {
int b[N] = {0};