Problem Solving and Program Design in C Chap. 6 Pointers and Modular Programming Chow-Sing Lin
Pointers float *p; – p as a pointer variable of type “pointer to float” int m = 25 ; int *itemp; itemp = &m; Dr. Chow-Sing LinModular Programming - CH 62
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 LinModular Programming - CH 63
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 LinModular Programming - CH 64
Pointers to Files (cont.) Read / write data from/to a file fscanf(inp, “%lf”, &item); fprintf(outp, “%.2f\n”, item); Close a file fclose(inp); fclose(outp); Dr. Chow-Sing LinModular Programming - CH 65
Dr. Chow-Sing LinModular Programming - CH 66
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 Lin7Modular Programming - CH 6
Functions with Simple Output Parameters (Cont.) Example 6.1 (Page281) – 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 Lin8Modular Programming - CH 6
Dr. Chow-Sing LinModular Programming - CH 69
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 LinModular Programming - CH 610
Dr. Chow-Sing LinModular Programming - CH 611
Dr. Chow-Sing LinModular Programming - CH 612
Dr. Chow-Sing LinModular Programming - CH 613 Data areas of main and separate Set up by the function call statement separate (value, &sn, &wh1, &fr) ;
Functions with Simple Output Parameters (Cont.) Example 6.1 (Cont.) – Effect of & Operator on the Data Type of a Reference Dr. Chow-Sing LinModular Programming - CH 614 DeclarationData Type of xData Type of &x char xcharchar * (pointer to char) int xintint * (pointer to int) double xdoubledouble * (pointer to double)
Functions with Simple Output Parameters (Cont.) The statements in function separate that cause the return of results follow. – 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 LinModular Programming - CH 615 *signp = ‘ - ’ ; *signp = ‘ ’ ; *signp = ‘ + ’ ; *wholep = floor(magnitude) ; *fracp = magnitude - *wholep ;
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 LinModular Programming - CH 616
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 LinModular Programming - CH 617
Exercises for section 6.1 Write a prototype for a function sum_n_avg – include Three type double input parameters Two output parameters – The function computes the sum and the average of its three input arguments and relays its results through two output parameters Dr. Chow-Sing LinModular Programming - CH 618 sum_n_avg sump avgp n3 n2 n1
Exercises for section 6.1 (Cont.) Dr. Chow-Sing LinModular Programming - CH 619 void sum_n_avg(double n1, /* input numbers */ double n2, double n3, double *sump, /* output –sum of the three numbers */ double *avgp ) /* output –average of the numbers */ Answer
Multiple Calls to a Function with Input/output Parameters 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 LinModular Programming - CH 620
Multiple Calls to a Function with Input/output Parameters (Cont.) Example 6.2 (Page289) – Main function gets three data values num1, num2, num3 – Sorting -- Increasing sequence The smallest value in num1 – The three calls to function order perform this sorting operation Dr. Chow-Sing LinModular Programming - CH 621
Dr. Chow-Sing LinModular Programming - CH 622
Dr. Chow-Sing LinModular Programming - CH 623
Multiple Calls to a Function with Input/output Parameters (Cont.) Example 6.2 (Cont.) – order(&num1, &num2) Stores the smaller of num1 and num2 in num1 The large in num2 – num1 = 7.5 ; num2 = 9.6 ; num3 = 5.5 order(&num1, &num2) – No change order(&num1, &num3) – Switches the values of num1 and num3 – num1 = 5.5 ; num3 = 7.5 Dr. Chow-Sing LinModular Programming - CH 624
Multiple Calls to a Function with Input/output Parameters (Cont.) Trace the main function execution Dr. Chow-Sing LinModular Programming - CH 625 Statementnum1num2num3Effect scanf(“…”, &num1, &num2, &num3); Enters data order(&num1, &num2);No change order(&num1, &num3); Switches num1 and num3 order(&num2, &num3); Switches num2 and num3 printf(“…”, num1, num2, num3);Displays
Dr. Chow-Sing LinModular Programming - CH 626 (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 LinModular Programming - CH 627
Exercises for Section 6.2 What would be the effect of the flowing sequence of calls to function order? – Hint : num1 = 8 ; num2 = 12 ; num3 = 10 – order(&num3, &num2); order(&num2, &num1); order(&num3, &num2); Dr. Chow-Sing LinModular Programming - CH 628
Exercises for Section 6.2 (Cont.) Answers Dr. Chow-Sing LinModular Programming - CH 629 Function callnum1num2num order(&num3, &num2); order(&num2, &num1);128 order(&num3, &num2);108 This sequence of calls to function order puts num1, num2, num3 in descending order (from largest to smallest).
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 LinModular Programming - CH 630
Scope of Names (Cont.) Dr. Chow-Sing LinModular Programming - CH 631
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 LinModular Programming - CH 632
Scope of Names (Cont.) NameVisible in oneVisible in fun_twoVisible in main MAXyes LIMITyes mainyes localvar (in main)no yes one (the function)yesnoyes anarg (int)yesno secondyesno onelocalyesno fun_twoyes one (formal parameter)noyesno anarg (char)noyesno localvar (in fun_two)noyesno Dr. Chow-Sing LinModular Programming - CH 633
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) Dr. Chow-Sing LinModular Programming - CH 634 Two output parameters Through function, returns the numerator and denominator of the function scanned
Formal Output Parameters as Actual Arguments (Cont.) Dr. Chow-Sing LinModular Programming - CH 635 Function scan_fraction needs to pass its output parameters to library function scanf which gets the needed numerator and denominator values
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 LinModular Programming - CH 636
Formal Output Parameters as Actual Arguments (Cont.) Dr. Chow-Sing LinModular Programming - CH 637 nump&slash denomp Nump and denomp store addresses, we can use them directly in the call to scanf
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 LinModular Programming - CH 638
Formal Output Parameters as Actual Arguments (Cont.) Data areas for scan_fraction and its Caller Dr. Chow-Sing LinModular Programming - CH 639
Passing an Argument x to Function some_fun Actual Argumen t Type Use in Calling Function Purpose in Called Function (some_fun) Formal Parameter Type Call to some_fun int char double local variable or input parameterinput parameter int char double some_fun(x) int char double local variable output or input/output parameter int * char * double * some_fun(&x) int * char * double * output or input/output parameter int * char * double * some_fun(x) int * char * double * output or input/output parameter input parameter int char double some_fun(*x) Dr. Chow-Sing LinModular Programming - CH 640
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
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 LinModular Programming - CH 646
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 LinModular Programming - CH 647
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 LinModular Programming - CH 648
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 LinModular Programming - CH 649
Common Programming Errors (Cont.) If an identifier is referenced outside its scope – An undeclared symbol syntax error will result Dr. Chow-Sing LinModular Programming - CH 650