Chapter 10 Pointer Applications

advertisement
This material is provided to the professors and students using Computer Science: A Structured Approach Using C,
Second Edition. All reprints in any form must cite this copyright.
Copyright(c) 2001 by Brooks/Cole
A division of Thomson Learning
Chapter 10 Pointer Applications
Review Questions
1.
a. True
2.
b. False
3.
a. True
4.
a. True
5.
c. The name of an array can be used with the indirection operator to reference data.
6.
b. Given a pointer, ptr, ptr + n is a pointer to the value n elements away.
7.
b. int ary[] [SIZE2]
8.
a. alloc()
9.
a. Allocated memory can be referred to only through pointers; it does not have its own identifier.
10.
b. calloc()
11.
a. To ensure that it is released, allocated memory should be freed before the program ends.
12.
c. Ragged arrays can only be used with arrays of integers.
Exercises
13.
a. *(tax + 6)
b. *(score + 7)
c. *(num + 4)
d. *(prices + 9)
14.
a. tax[4]
b. score[2]
c. number[0]
d. prices[0]
15.
If we interpret the sixth element as ary[5], and if p is pointing to ary[3], we can access ary[5] using *(p+2), or
we can use an index, like so: p[2].
16.
void fun (float *ary, float *last);
== or ==
void fun (float ary[], float *last);
/* Local Definitions */
int ary[200];
int *p = &ary[199];
17.
18.
19.
/* Function calling */
fun (ary, p);
== or ==
fun (ary, &ary[199]);
66
34
62
46
6
1
6
Prototype: void fun (int **ary);
Call: fun (table);
Page 10.1
20.
21.
void torture (int *row);
See Figure 10.1.
Figure 10.1 Solution for Chapter 10, Exercise 21.
22.
23.
a. x is an array of pointers where each pointer can point to an integer.
b. x is a pointer to an array of integers.
769
47
See Figure 10.2.
Figure 10.2 Solution for Chapter 10, Exercise 23
452
769
Explanation for the first call:
p is pointing to the whole first row, so (*p) is the first row itself. Then using the first row, (*p)[0] refers to the
first element (4), (*p)[1] refers to the second element (5), and (*p)[2] refers to the third element (2).
24.
25.
26.
27.
Explanation for the second call:
p is pointing to the whole second row (x+1), so (*p) is the second row itself. Then using the second row,
(*p)[0] refers to the first element (7), (*p)[1] refers to the second element (6), and (*p)[2] refers to the third
element (9).
a. (address of num[0])
b. 23
c. 24
d. 3
e. 27
f. 2
a. e
b. m
a. 2147462972 (address of data[4])
b. 1
c. 9
d. 2
a. 4
b. 4
c. 2147462972 (address of i)
d. 4
e. 2147462972 (address of i)
f. 2147466300 ((address of i) + 8)
Page 10.2
28.
29.
30.
31.
32.
33.
34.
35.
a. *a
b. *(a + 5)
c. a - 1
d. a + 19
e. a + 20
f. *(a + 4)
g. *(a + 13)
h. *(a + j + 1)
a. num[2]
b. num[j]
c. num[i + j]
d. num[i] + num[j]
e. num[num[1]]
a. num
b. &num[0]
a. &num[0]
b. num[0]
c. num[0] + 1
d. num[1]
e. num[j]
if (pn > &num[25])
…
a. not valid: mushem needs two addresses.
b. not valid: mushem needs two addresses.
c. valid
d. valid
e. valid
1. 4 17 9
2. 35 17 14 19 8 20 11
1. 9 4 17
2. 31 17 10 18 7 19 10
Problems
36.
// ===================== checkData ====================
This function checks if every element of array 1 is
equal to its corresponding element in array 2.
Pre
a pointer to array 1 and
a pointer to array 2
Post
returns 0 if arrays equal, 1 if different
*/
int checkData (int *pAry1, int *pAry2)
{
/* Local Definitions */
int *p1;
int *p2;
int *pLast;
int chk;
/* Statements */
chk
= 0;
pLast = p1 + 9;
for (p1 = pAry1, p2 = pAry2;
p1 <= pLast;
p1++, p2++)
{
Page 10.3
if (*p1 != *p2)
chk = 1;
} /* for */
37.
}
/*
return (chk);
/* checkData */
============ checkData ================
This function checks if every element
equal to its corresponding element in
Pre
pointer to array 1 and
pointer to array 2
integer containing number of
Post returns 0 if arrays equal, 1
of array 1 is
array 2.
array elements
if different
*/
int checkData (int *pAry1, int *pAry2, int size)
{
/* Local Definitions */
int *p1;
int *p2;
int *pLast;
int chk;
/* Statements */
chk = 0;
pLast = p1 + size – 1;
for (p1 = pAry1, p2 = pAry2;
p1 <= pLast;
p1++, p2++)
{
if (*p1 != *p2)
chk = 1;
} /* for */
}
return (chk);
/* checkData */
38.
/* =============== reverse ====================
This function reverses the elements of an array.
Pre
pointer to an array
(array size must be known -- we assume 10)
Post elements in array have been reversed.
*/
void reverse (int *ary)
{
/* Local Definitions */
int i;
int j;
int temp;
/* Statements */
for (i = 0, j =
{
temp
*(ary + i)
*(ary + j)
} /* for */
9; i < j; i++, j--)
= *(ary + i);
= *(ary + j);
= temp;
Page 10.4
}
return;
/* reverse */
39.
/* ============ Pascal ===================
This function creates a 2-dimentional ragged array
representing the Pascal Triangle.
Pre
size of triangle as an integer
Post returns pointer to ragged array
*/
int ** Pascal (int size)
{
/* Local Definitions */
int **p;
int
row;
int
col;
/* Statements */
/* allocation for the array for ragged array */
p = (int **) calloc (size + 2, sizeof (int *));
*(p + size + 1) = NULL;
for (row = 0; row < size + 1; row++)
*(p + row) = (int *) calloc (row + 1, sizeof (int));
}
/* Filling data according to the size of triangle */
for (row = 0; row < size + 1; row++)
{
for (col = 0; col < row + 1; col++)
{
if (row == col || col == 0)
*(*(p + row) + col) = 1;
else
*(*(p + row) + col) =
*(*(p + row - 1) + (col - 1)) +
*(*(p + row - 1) + col);
} /* for col */
} /* for row */
return p;
/* Pascal */
40.
/* ================= ISBNtest =================
This function tests an ISBN to see if it is valid.
It accepts a pointer value (array name) and returns
zero for invalid and non-zero for valid data
Pre the array of ISBN code to be checked
Post returns zero for invalid: non-zero for valid
*/
int ISBNtest (char *isbn)
{
/* Local Definitions */
char *pcur;
char *plast;
int
weight;
int
sum = 0;
/* Statements */
plast = isbn + 9;
Page 10.5
for (pcur = isbn, weight = 10;
pcur <= plast;
pcur++, weight--)
{
/* when the 10th digit (isbn[9]) is 'x') */
if (pcur == plast && *pcur == 'x')
sum += 10;
else
/* ASCII - 48 = value numeric character */
sum += ((int)(*pcur) - 48) * weight;
} /* for */
}
return (sum % 11
/* ISBNtest */
==
0);
41.
/* =========== convertArray =============
This function copies a 1-dimentional array of n
elements into a 2-dimentional array of j rows and
k columns.
Pre One-dimensional array
Number of elements in one-dimensional array
Number of rows in two-dimensional array
Number of columns in two-dimensional array
Post if elements != toRow * toCol, returns null
else, returns pointer to 2-dimentional array
*/
int** convertArray int *fromAry, int elements,
int toRow,
int toCol)
{
/* Local Definitions */
int **twoDimAry;
int **row;
int **lastRow;
int *col;
int *lastCol;
int
fromIndex = 0;
/* Statements */
if (elements != toRow * toCol)
twoDimAry = NULL;
else
{
twoDimAry = (int**)calloc(toRow, sizeof (int*));
lastRow = twoDimAry + toRow – 1;
for (row = twoDimAry; row <= lastRow; row++)
{
*row = (int*)calloc(toCol, sizeof (int));
lastCol = *row + toCol – 1;
for (col = *row;
col <= lastCol;
fromIndex++, col++)
*col = *(fromAry + fromIndex);
} /* for row */
} /* if else */
}
return twoDimAry;
/* convertArray */
Page 10.6
42.
/* Local Definitions */
int num[20];
int *walker;
int *plast;
/* Statements */
plast = num + 19;
for (walker = num; walker <= plast; walker++)
{
printf ("\nPlease enter integer: ");
scanf ("%d", walker);
} /* for */
43.
/* Local Definitions */
char a[40];
char *walker;
char *plast;
/* Statements */
plast = a + 39;
for (walker = a; walker <= plast; walker++)
{
printf ("\nPlease enter character: ");
scanf ("%d", walker);
} /* for */
44.
/* Local Definitions */
char temp;
char a[6] = {‘z’, ‘x’, ‘m’, ‘s’, ‘e’, ‘h’};
char *walker;
char *plast;
/* Statements */
plast = a + 5;
temp = *plast;
for (walker = plast; walker > a; walker--)
*walker = *(walker – 1);
*walker = temp;
45.
/* Local Definitions */
char temp;
char a[6] = {‘z’, ‘x’, ‘m’, ‘s’, ‘e’, ‘h’};
char *walker;
char *plast;
/* Statements */
plast = a + 5;
temp = *a;
for (walker = a; walker < plast; walker++)
*walker = *(walker + 1);
*walker = temp;
46.
/* ================ addem =================
This function adds 2 to the first parameter and 5 to
the second parameter.
Pre
the address of 2 integers
Page 10.7
Post values in parameters changed
*/
void addem (int *x, int *y)
{
/* Statements */
*x += 2;
*y += 5;
return;
} /* addem */
Page 10.8
Download