Problem Solving and Program Design in C Chap
Problem Solving and Program Design in C Chap. 6 Pointers and Modular Programming Chow-Sing Lin
CSIE@NUTN Pointers • float *p; – p as a pointer variable of type “pointer to float” int m = 25 ; int *itemp; itemp = &m; Dr. Chow-Sing Lin Modular Programming - CH 6 2
CSIE@NUTN Indirect Reference • When the unary indirection operator * is applied to a pointer variable, it has the effect of following the pointer referenced by its operand. – Indirect reference • Example 6. 2 *itemp = 35; Printf(“%d”, *itemp); *itemp = 2 * (*itemp); Dr. Chow-Sing Lin Modular Programming - CH 6 3
CSIE@NUTN Pointers to Files • Declare that file pointer variable inp and outp will hold information allowing access to the program’s input and output files. FILE *inp; FILE *outp; • Open a file inp = fopen(“distance. txt”, ”r”); outp = fopen(“distout. txt”, “w”); Dr. Chow-Sing Lin Modular Programming - CH 6 4
CSIE@NUTN Pointers to Files (cont. ) • Read / write data from/to a file fscanf(inp, “%lf”, &item); fprintf(outp, “%. 2 fn”, item); • Close a file fclose(inp); fclose(outp); Dr. Chow-Sing Lin Modular Programming - CH 6 5
CSIE@NUTN Dr. Chow-Sing Lin Modular Programming - CH 6 6
CSIE@NUTN Functions with Simple Output Parameters • Argument lists – Provide the communication links between the main function and its function subprograms – Enable a function to manipulate different data each time it is called • When a function call executes, – Computer allocates memory space in the function data area for each formal parameter Dr. Chow-Sing Lin Modular Programming - CH 6 7
CSIE@NUTN Functions with Simple Output Parameters (Cont. ) • Example 6. 1 (Page 281) – Function separate finds the sign, whole number magnitude, and fractional parts of its first parameter – All the formal parameters of a function represent inputs to the function from the calling function – In function separate • Input – num • Output – signp – wholep – fracp • Used to carry multiple results from function separate back to the function calling it Dr. Chow-Sing Lin Modular Programming - CH 6 8
CSIE@NUTN Dr. Chow-Sing Lin Modular Programming - CH 6 9
CSIE@NUTN Functions with Simple Output Parameters (Cont. ) • Example 6. 1 (Cont. ) – Output parameter : char *signp (char * signp) • Tells the compiler that signp will contain the address of a type chat variable • pointer – signp is the address of a type char variable (the parameter signp is a pointer to a type char variable) – wholep and fracp are pointers to variables of types int and double Dr. Chow-Sing Lin Modular Programming - CH 6 10
CSIE@NUTN Dr. Chow-Sing Lin Modular Programming - CH 6 11
CSIE@NUTN Dr. Chow-Sing Lin Modular Programming - CH 6 12
CSIE@NUTN Data areas of main and separate Set up by the function call statement separate (value, &sn, &wh 1, &fr) ; Dr. Chow-Sing Lin Modular Programming - CH 6 13
CSIE@NUTN Functions with Simple Output Parameters (Cont. ) • Example 6. 1 (Cont. ) – Effect of & Operator on the Data Type of a Reference Declaration Data Type of x Data Type of &x char * (pointer to char) int x int * (pointer to int) double x double * (pointer to double) Dr. Chow-Sing Lin Modular Programming - CH 6 14
CSIE@NUTN Functions with Simple Output Parameters (Cont. ) • The statements in function separate that cause the return of results follow. *signp = ‘ - ’ ; *signp = ‘ + ’ ; *wholep = floor(magnitude) ; *fracp = magnitude - *wholep ; – When the unary * operator is applied to reference that is of some pointer type, it has the effect of following the pointer referenced by its operand. Dr. Chow-Sing Lin Modular Programming - CH 6 15
CSIE@NUTN Functions with Simple Output Parameters (Cont. ) • Difference – a direct reference to a variable of type “pointer to int” – An indirect or “pointer-following” reference to the same variable using indirection operator Dr. Chow-Sing Lin Modular Programming - CH 6 16
CSIE@NUTN Meaning of * symbol • “*” in the declarations of the function’s formal – Multiplication – Pointer to • char *signp • Tell the compiler that the type of parameter signp is “pointer to char” (declaration) – Unary indirection operator • Follow the pointer • *signp – follow the pointer in signp • Do not confuse the meaning of * in a reference with its meaning in a parameter declaration. Dr. Chow-Sing Lin Modular Programming - CH 6 17
CSIE@NUTN Exercises for section 6. 1 • Write a prototype for a function sum_n_avg – include • Three type double input parameters • Two output parameters n 1 n 2 n 3 sum_n_avg sump avgp – The function computes the sum and the average of its three input arguments and relays its results through two output parameters Dr. Chow-Sing Lin Modular Programming - CH 6 18
CSIE@NUTN Exercises for section 6. 1 (Cont. ) Answer void sum_n_avg(double n 1, /* input numbers */ double n 2, double n 3, double *sump, /* output –sum of the three numbers */ double *avgp ) /* output –average of the numbers */ Dr. Chow-Sing Lin Modular Programming - CH 6 19
Multiple Calls to a Function with Input/output Parameters CSIE@NUTN • Use of a single parameter • bring a data value into a function • Carry a result value out of the function • A function may be called more than once in a given program and process different data in each call Dr. Chow-Sing Lin Modular Programming - CH 6 20
Multiple Calls to a Function with Input/output Parameters (Cont. ) CSIE@NUTN • Example 6. 2 (Page 289) – Main function gets three data values • num 1, num 2, num 3 – Sorting -- Increasing sequence • The smallest value in num 1 – The three calls to function order perform this sorting operation Dr. Chow-Sing Lin Modular Programming - CH 6 21
CSIE@NUTN Dr. Chow-Sing Lin Modular Programming - CH 6 22
CSIE@NUTN Dr. Chow-Sing Lin Modular Programming - CH 6 23
Multiple Calls to a Function with Input/output Parameters (Cont. ) CSIE@NUTN • Example 6. 2 (Cont. ) – order(&num 1, &num 2) • Stores the smaller of num 1 and num 2 in num 1 • The large in num 2 – num 1 = 7. 5 ; num 2 = 9. 6 ; num 3 = 5. 5 • order(&num 1, &num 2) – No change • order(&num 1, &num 3) – Switches the values of num 1 and num 3 – num 1 = 5. 5 ; num 3 = 7. 5 Dr. Chow-Sing Lin Modular Programming - CH 6 24
Multiple Calls to a Function with Input/output Parameters (Cont. ) CSIE@NUTN • Trace the main function execution Statement num 1 scanf(“…”, &num 1, &num 2, &num 3); 7. 5 num 2 num 3 Effect 9. 6 5. 5 Enters data order(&num 1, &num 2); No change order(&num 1, &num 3); 5. 5 9. 6 7. 5 Switches num 1 and num 3 order(&num 2, &num 3); 5. 5 7. 5 9. 6 Switches num 2 and num 3 printf(“…”, num 1, num 2, num 3); Dr. Chow-Sing Lin Displays 5. 5 7. 5 9. 6 Modular Programming - CH 6 25
CSIE@NUTN (7. 5 > 5. 5) true! Cause the 7. 5 to be copied into the local variable temp Cause the 7. 5 in the variable pointed to by smp to be replacer by 5. 5 Copies temp variable 7. 5 into the variable pointed by lgp Dr. Chow-Sing Lin Modular Programming - CH 6 26
CSIE@NUTN Dr. Chow-Sing Lin Modular Programming - CH 6 27
CSIE@NUTN Exercises for Section 6. 2 • What would be the effect of the flowing sequence of calls to function order? – Hint : num 1 = 8 ; num 2 = 12 ; num 3 = 10 – order(&num 3, &num 2); order(&num 2, &num 1); order(&num 3, &num 2); Dr. Chow-Sing Lin Modular Programming - CH 6 28
CSIE@NUTN Exercises for Section 6. 2 (Cont. ) • Answers Function call num 1 num 2 num 3 8 12 10 12 8 order(&num 3, &num 2); order(&num 2, &num 1); order(&num 3, &num 2); 10 8 This sequence of calls to function order puts num 1, num 2, num 3 in descending order (from largest to smallest). Dr. Chow-Sing Lin Modular Programming - CH 6 29
CSIE@NUTN Scope of Names • Scope of a name – Refers to the region of a program where a particular meaning of a name is visible or can be referenced. Dr. Chow-Sing Lin Modular Programming - CH 6 30
CSIE@NUTN Scope of Names (Cont. ) Dr. Chow-Sing Lin Modular Programming - CH 6 31
CSIE@NUTN Scope of Names (Cont. ) • Function fun_two can be called by functions one, main, and itself • Function one can be called by the main function and itself – But not by function fun_two – Because one is used as a formal parameter name in function fun_two Dr. Chow-Sing Lin Modular Programming - CH 6 32
CSIE@NUTN Scope of Names (Cont. ) Name Visible in one Visible in fun_two Visible in main MAX yes yes LIMIT yes yes main yes yes localvar (in main) no no yes one (the function) yes no yes anarg (int) yes no no second yes no no onelocal yes no no fun_two yes yes one (formal parameter) no yes no anarg (char) no yes no localvar (in fun_two) no yes no Dr. Chow-Sing Lin Modular Programming - CH 6 33
CSIE@NUTN Formal Output Parameters as Actual Arguments • When actual argument calls another function – a function needs to pass its own output parameter as an argument • Function scan_fraction (not completed) Two output parameters Dr. Chow-Sing Lin Through function, returns the numerator and denominator of the function scanned Modular Programming - CH 6 34
CSIE@NUTN Formal Output Parameters as Actual Arguments (Cont. ) Function scan_fraction needs to pass its output parameters to library function scanf which gets the needed numerator and denominator values Dr. Chow-Sing Lin Modular Programming - CH 6 35
CSIE@NUTN Formal Output Parameters as Actual Arguments (Cont. ) • Scans a data line representing a common fraction of the form – numerator / denominator • numerator is an integer • denominator is a positive integer • The / symbol is a separator • Use function get_int – Outer loop repeats until a valid fraction is scanned – Inner loop skips any characters at the end of the data line Dr. Chow-Sing Lin Modular Programming - CH 6 36
CSIE@NUTN Formal Output Parameters as Actual Arguments (Cont. ) nump &slash denomp Nump and denomp store addresses, we can use them directly in the call to scanf Dr. Chow-Sing Lin Modular Programming - CH 6 37
CSIE@NUTN Formal Output Parameters as Actual Arguments (Cont. ) • scanf(“%d %c%d”, nump, &slash, denomp); – scanf stores the first number scanned in the variable whose address is in nump – The slash character (possibly preceded by blanks) in local variable slash – The second number scanned in the variable whose address is in denomp – if statement validates the fraction • If the data entry was unsuccessful – Setting the flag error to 1(true) Dr. Chow-Sing Lin Modular Programming - CH 6 38
CSIE@NUTN Formal Output Parameters as Actual Arguments (Cont. ) • Data areas for scan_fraction and its Caller Dr. Chow-Sing Lin Modular Programming - CH 6 39
CSIE@NUTN Passing an Argument x to Function some_fun Actual Argumen Use in Calling Function t Type Purpose in Called Function (some_fun) Formal Call to Parameter some_fun Type local variable or input parameter int char double some_fun(x) local variable output or input/output parameter int * char * double * some_fun(&x) int * output or char * output or input/output parameter input/output double * parameter int * char * double * some_fun(x) int * char * output or input/output parameter input parameter double * int char double some_fun(*x) int char double Dr. Chow-Sing Lin Modular Programming - CH 6 40
Figure 6. 12 Structure Chart for Computing Solar Collecting Areas Size 1 -41
Figure 6. 13 Program to Approximate Solar collecting Area Size 1 -42
Figure 6. 13 Program to Approximate Solar collecting Area Size (cont’d) 1 -43
Figure 6. 13 Program to Approximate Solar collecting Area Size (cont’d) 1 -44
Figure 6. 13 Program to Approximate Solar collecting Area Size (cont’d) 1 -45
CSIE@NUTN Debugging and Testing a Program System • Top-down testing – If a program contains one or more stubs • The message printed by each stub when it is called provides a trace of the call sequence and allows the programmer to determine whether the flow of control within the program is correct – Between a main function and its subordinate functions Dr. Chow-Sing Lin Modular Programming - CH 6 46
CSIE@NUTN Debugging and Testing a Program System (Cont. ) • Unit test – When a function is completed • it can be substituted for its stub in the program • We often perform a preliminary test of a new function before substitution – Because it is easier to locate and correct errors when dealing with a single function – A test of an individual function Dr. Chow-Sing Lin Modular Programming - CH 6 47
CSIE@NUTN Debugging and Testing a Program System (Cont. ) • Bottom-up testing – The process of separately testing individual functions before inserting them in a program system • System integration tests – Testing a system after replacing all its stubs with functions that have been pretested Dr. Chow-Sing Lin Modular Programming - CH 6 48
CSIE@NUTN Common Programming Errors • Each actual input argument must be of a type assigned to its corresponding formal parameter • Each actual output argument must be of the same pointer data type as the corresponding formal parameter • If an output parameter is not of a pointer type or if the calling function neglects to send a correct variable address – The function results will be incorrect Dr. Chow-Sing Lin Modular Programming - CH 6 49
CSIE@NUTN Common Programming Errors (Cont. ) • If an identifier is referenced outside its scope – An undeclared symbol syntax error will result Dr. Chow-Sing Lin Modular Programming - CH 6 50
- Slides: 50