Sams Teach Yourself C in 21 Days

(singke) #1
Pointers: Beyond the Basics 395

4: #include <stdio.h> 15
5:
6: void printarray_1(int (*ptr)[4]);
7: void printarray_2(int (*ptr)[4], int n);
8:
9: int main( void )
10: {
11: int multi[3][4] = { { 1, 2, 3, 4 },
12: { 5, 6, 7, 8 },
13: { 9, 10, 11, 12 } };
14:
15: /* ptr is a pointer to an array of 4 ints. */
16:
17: int (*ptr)[4], count;
18:
19: /* Set ptr to point to the first element of multi. */
20:
21: ptr = multi;
22:
23: /* With each loop, ptr is incremented to point to the next */
24: /* element (that is, the next 4-element integer array) of multi. */
25:
26: for (count = 0; count < 3; count++)
27: printarray_1(ptr++);
28:
29: puts(“\n\nPress Enter...”);
30: getchar();
31: printarray_2(multi, 3);
32: printf(“\n”);
33: return 0;
34: }
35:
36: void printarray_1(int (*ptr)[4])
37: {
38: /* Prints the elements of a single four-element integer array. */
39: /* p is a pointer to type int. You must use a type cast */
40: /* to make p equal to the address in ptr. */
41:
42: int *p, count;
43: p = (int *)ptr;
44:
45: for (count = 0; count < 4; count++)
46: printf(“\n%d”, *p++);
47: }
48:
49: void printarray_2(int (*ptr)[4], int n)
50: {
51: /* Prints the elements of an n by four-element integer array. */
52:

LISTING15.4 continued

25 448201x-CH15 8/13/02 11:13 AM Page 395

Free download pdf