/*Compute average of an array, array subscripting version. * double table average (int a[], int n double sum =0.0;/* running total in七 / count of items * for(主=0;i<n;i++) sum + a[ili return (n ! 0)? sum /n: 0.0; /* Compute average of an array, pointer version. * double table average(int a[], int n) double sum =0.0;/* running total in七 1; / count of items * in七 ptri /* traversing pointer * ptr for(主=0;i<n;i++) I sum + *ptri ptr++i return (n ! 0)? sum /n: 0.0; PROGRAM MINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu ol Hone Kone
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu /*Compute average of an array, array subscripting version.*/ double table_average(int a[], int n) { double sum = 0.0; /* running total */ int i; /* count of items */ for (i = 0; i < n; i++) sum += a[i]; return (n != 0) ? sum / n : 0.0; } /* Compute average of an array, pointer version. */ double table_average(int a[], int n) { double sum = 0.0; /* running total */ int i; /* count of items */ int *ptr; /* traversing pointer */ ptr = a; for (i = 0; i < n; i++) { sum += *ptr; ptr++; } return (n != 0) ? sum / n : 0.0; }
Pointer Arithmetic the only legal; arithmetic operators on pointers are adding and subtracting an integer, or subtracting one pointer from another p is &t[o], q is &t[3], then g-p is 3 how to find t[n/2 suppose, minptr points to the array' s first element maxptr points to the array s last element ((minptr+maxptr )/2)is illegal as adding to pointer is not allowed how about (minptr+(maxptr-minptr)/2) as(maxptr-minptr)/2 is an integer PROGRAM MINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu Pointer Arithmetic • the only legal; arithmetic operators on pointers are adding and subtracting an integer, or subtracting one pointer from another p is &t[0], q is &t[3], then q-p is 3 how to find t[n/2] suppose, minptr points to the array’s first element maxptr points to the array’s last element *((minptr+maxptr)/2) is illegal as adding to pointer is not allowed. how about *(minptr + (maxptr-minptr)/2) as (maxptr-minptr)/2 is an integer
Pointer Comparison pointers can be compared with the logical operators p is &tU], q is &t[k], if j<k, then p<q Don't compare pointers that don't access the same array PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu Pointer Comparison • pointers can be compared with the logical operators (==, !=, <. <=, >, >=) p is &t[j], q is &t[k], if j < k, then p < q • Don’t compare pointers that don’t access the same array
/ Compute average of an array concise pointer version. * double table average(int a[], int n) double sum =0.0; /* running total * int ptri /* traversing pointer * int *endptr a n; / pointer to just past end * for (ptr ai ptr endptri ptr++) sum + *ptri return (n 0)? sum/n: 0.0 int *endptr atn it does not mean endptr +n; but endptr a+n 七 endptr a+ n; it assign a n to endptr PROGRAM MINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu ol Hone Kone
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu /* Compute average of an array, concise pointer version.*/ double table_average(int a[], int n) { double sum = 0.0; /* running total */ int *ptr; /* traversing pointer */ int *endptr = a + n; /* pointer to just past end */ for (ptr = a; ptr < endptr; ptr++) sum += *ptr; return (n != 0) ? sum / n : 0.0; } int *endptr = a + n; it does not mean *endptr = a + n; but endptr = a + n; int *endptr = a + n; it assign a + n to endptr
ptr +t Example while(ptr endptr) sum+=ptr++ /*it means obtaining the value of the pointer ptr points to and add to sum, then increment the pointer ptr by one it is equivalentto sum+=ptr ptr++, PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu *ptr ++ Example while (ptr < endptr) sum += *ptr++; /* it means obtaining the value of the pointer ptr points to and add to sum, then increment the pointer ptr by one. */ it is equivalent to sum += *ptr; ptr++;