Pointer version of input-reversal program define MAXVALS 100 /* max values in table * int main o int table fill(int all int max)i void table print rev(int a[], int num)i int t[MAxvAlS]; /* input values*/ int n table fill(t, MAXvALS) table print rev(tr n)i return 0; PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu /* * Pointer version of input-reversal program. */ #define MAXVALS 100 /* max values in table */ int main() { int table_fill(int a[], int max); void table_print_rev(int a[], int num); int t[MAXVALS]; /* input values*/ int n = table_fill(t, MAXVALS); table_print_rev(t, n); return 0; }
/*Pointer versions of table-handling functions ★七tab1efi11- ead va1 ues into table *table print rev -print values in reverse order. * int table fill(int a[] int max t int *ptr a /* pointer to first element * int *endptr ptr max; / pointer to just past 1 ast elemen七 for (ptr a; ptr endptri ptr++) if (scanf("i", ptr)!= 1) break return ptr -a;/* of values read successfully * void table print rev (int a[], int num) int *ptr a num;/* pointer to just past last element * while (ptr--> a) printf("i\n", *ptr); PROGRAM MINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu ol Hone Kone
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu /*Pointer versions of table-handling functions. *table_fill - read values into table. *table_print_rev - print values in reverse order. */ int table_fill(int a[], int max) { int *ptr = a; /* pointer to first element */ int *endptr = ptr + max; /* pointer to just past last element */ for (ptr = a; ptr < endptr; ptr++) if (scanf("%i", ptr) != 1) break; return ptr - a; /* # of values read successfully */ } void table_print_rev(int a[], int num) { int *ptr = a + num; /* pointer to just past last element */ while (ptr-- > a) printf("%i\n", *ptr); }
Array Parameters and Pointers double table average (int a[ ], int n)i is equivalent to double table average (int *a int n)i /*to compute the average of k elements of t, staring with t[]*/ avg= table_average &t[], k) avg= table_average(t+i, k) avg= table_average(t O], k); /wrong*/ 港城市大 PROGRAMMING METHDOLOGY AND SOFTWAREENGINEERING Copyrighto1998 Angus Wu ol Hone Kone
PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu Array Parameters and Pointers double table_average(int a [ ], int n); is equivalent to double table_average(int *a, int n); /* to compute the average of k elements of t, staring with t[i] */ avg = table_average(&t[i], k); avg = table_average(t + i, k); avg = table_average(t[i], k); /* wrong */
Pointer conversion double f[MAxvAS], g [MAXvALS]i f intends to copy array g to f; *assignment doesn 't work * instead use memcpy(f, g, sizeof(g))i 港城市大 PROGRAMMING METHDOLOGY AND SOFTWAREENGINEERING Copyrighto1998 Angus Wu ol Hone Kone
PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu Pointer Conversion double f[MAXVALS], g[MAXVALS]; f = g; intends to copy array g to f; *assignment doesn’t work */ instead use memcpy(f, g, sizeof(g));
DYNAMICALLY MEMORY ALLOCATION PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu DYNAMICALLY MEMORY ALLOCATION