/* * Example of function with simple output parameters * Figures 6.3, page 283, Hanly and Koffman, 5ed. * */ #include #include void separate(double num, char *signp, int *wholep, double *fracp); int main() { double value; char sn; int whl; double fr; printf("Please input the value = "); scanf("%lf",&value); separate(value, &sn, &whl, &fr); /* the address of ... */ printf("Parts of %.4f \n",value); printf(" sign : %c\n",sn); printf(" whole number magnitude : %d\n",whl); printf(" fractional parts : %.4f\n",fr); return 0; } /* ---------------------------------------------------------------- */ /* Separate a number into three parts: * sign (+, -, or blank), a whole number magnitude, and a fractional part * * Pre: num is defined; signp, wholep, fracp contain address of memory cells * where results are to be stored * Post: results are stored in the cells pointed by signp, wholep, fracp */ void separate(double num, char *signp, int *wholep, double *fracp) { double magnitude; if (num < 0) *signp = '-'; else if (num == 0) *signp = ' '; else *signp = '+'; magnitude = fabs(num); *wholep = floor(magnitude); *fracp = magnitude - *wholep; printf("\nwhat is *wholep = %d",*wholep); printf("\nwhat is *fracp = %f",*fracp); } // // Remarks: // // (1) char *signp,: // signp will contain the address of a type char variable, // or say "signp is a pointer to a type char variable" // // (2) int *wholep: // wholep is a pointer to a type int variable. // // (3) double *fracp: // fracp is a pointer to a type double variable. // // (4) no return statement, void! // // (5) Three meanings of * symbol // (a) multiplication, as binary operator // a*b, // (b) ``pointer to'', in the declaration // (c) unary indirection operator, ``follow the pointer', in function body // // (6) pointer: a memory cell whose content is the address of another // memory cell. //