0% found this document useful (0 votes)
61 views

Pointers and Modular Programming: Mirza Mohammad Lutfe Elahi

The document discusses pointers and modular programming in C. It outlines topics including pointer variables, the address and indirection operators, functions with output parameters, multiple calls to functions, scope of names, and file input/output. Examples are provided to demonstrate pointer initialization with the address operator, indirect reference to access and modify variables, the triple use of the asterisk operator, passing parameters to functions, and tracing a program to sort three numbers.

Uploaded by

Rahib ridoy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Pointers and Modular Programming: Mirza Mohammad Lutfe Elahi

The document discusses pointers and modular programming in C. It outlines topics including pointer variables, the address and indirection operators, functions with output parameters, multiple calls to functions, scope of names, and file input/output. Examples are provided to demonstrate pointer initialization with the address operator, indirect reference to access and modify variables, the triple use of the asterisk operator, passing parameters to functions, and tracing a program to sort three numbers.

Uploaded by

Rahib ridoy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Pointers and Modular

Programming

Mirza Mohammad Lutfe Elahi


CSE 115 Programming Language I ECE@NSU
2

Outline
• Pointer Variables
• Address Operator and Indirect Reference
• Functions with Output Parameters
• Multiple Calls to a Function
• Scope of Names
• File Input and Output
• Common Programming Errors

CSE 115 Programming Language I ECE@NSU


3

Address Operator
• How to initialize a pointer variable?
• We can use the address operator &
• Example:
int m = 25;
int *itemp; /* pointer variable */
itemp = &m; /* Store address of m in pointer
itemp */

CSE 115 Programming Language I ECE@NSU


4

Indirect Reference (De-Reference)


We can access and modify a variable:
1. Either directly using the variable name
2. Or indirectly, using a pointer to the variable
• Example:
int m = 25;
int *itemp; /* pointer variable */
itemp = &m; /* Store address of m in pointer
itemp */
*itemp = 35; /* m = 35 */
printf(“%d”, *itemp);
CSE 115 Programming Language I ECE@NSU
5

Triple Use of * (Asterisk)


1. As a multiplication operator:
z = x * y ; /* z = x times y */
2. To declare pointer variables:
char ch; /* ch is a character */
char *p; /* p is pointer to char */
3. As an indirection operator:
p = &ch; /* p = address of ch */
*p = 'A'; /* ch = 'A' */
*p = *p + 1; /* ch = 'A' + 1 = 'B' */
CSE 115 Programming Language I ECE@NSU
6
&p:2293312 &d:2293320
Example p=2293320 d = 13.5
#include <stdio.h>
int main(void) {
double d = 13.5;
double *p; /* p is a pointer to double */
p = &d; /* p = address of d */
printf("Value of d = %.2f\n", d);
printf("Value of &d = %d\n", &d);
printf("Value of p = %d\n", p);
printf("Value of *p = %.2f\n", *p);
printf("Value of &p = %d\n", &p);
*p = -5.3; /* d = -5.3 */
printf("Value of d = %.2f\n", d);
return 0;
}
CSE 115 Programming Language I ECE@NSU
Pointer &p:2293312 &d:2293320
7

p=2293320 d = 13.5
Summary
• Using a pointer variable p, one can access:
1. Its direct value: the value of pointer variable p
– In the example, the value of p is 2293320
– It is the address of variable d (&d is 2293320)

2. Its indirect value: using the indirection operator *


– In the example, *p is the value of d, which is 13.5

3. Its address value: using the address operator &


– In the example, &p is 2293312

CSE 115 Programming Language I ECE@NSU


8

Function with Output Parameter


• So far, we know how to:
– Pass input parameters to a function
– Use the return statement to return one function result
• Functions can also have output parameters
– To return multiple results from a function
• Output parameters are pointer variables
– The caller passes the addresses of variables in memory
– The function uses indirect reference to modify variables in
the calling function (for output results)

CSE 115 Programming Language I ECE@NSU


9

Example: Function separate


 Writea function that separates a number into a sign, a
whole number magnitude, and a fractional part.

void separate /* function separate */


(double num, /* input number */
char *signp, /* sign pointer */
int *wholep, /* whole number pointer */
double *fracp); /* fraction pointer */
CSE 115 Programming Language I ECE@NSU
10

CSE 115 Programming Language I ECE@NSU


11

CSE 115 Programming Language I ECE@NSU


12

Parameter Passing for Function


separate

CSE 115 Programming Language I ECE@NSU


13

Sort 3 Numbers

CSE 115 Programming Language I ECE@NSU


14

CSE 115 Programming Language I ECE@NSU


15

Tracing Program: Sort 3 Numbers

Statement num1 num2 num3 Effect

scanf(. . .); 7.5 9.6 5.5 Input Data

order(&num1, &num2); 7.5 9.6 5.5 No change

order(&num1, &num3); 5.5 9.6 7.5 swap num1, num3

order(&num2, &num3); 5.5 7.5 9.6 swap num2, num3

printf(. . .); 5.50 7.50 9.60

CSE 115 Programming Language I ECE@NSU


16

Trace: order($num1, &num3)

Data area after: temp = *smp;


CSE 115 Programming Language I ECE@NSU
17

Scope of a Name
• Region of program where a name is visible
• Region of program where a name can be referenced
• Scope of: #define NAME value
– From the definition line until the end of file
– Visible to all functions that appear after #define
• Scope of a function prototype
– Visible to all functions defined after the prototype
• Scope of a parameter and a local variable
– Visible only inside the function where it is defined
– Same name can be re-declared in different functions
CSE 115 Programming Language I ECE@NSU
18
MAX and LIMIT are visible to all functions

prototypes are typically


visible to all functions

function one is not visible to fun_two: has parameter one

localvar is visible inside main only

anarg, second, and onelocal are


visible inside function one only

one, anarg, and localvar are


visible inside fun_two only

CSE 115 Programming Language I ECE@NSU


19

Why Data Files?


• So far, all our examples obtained their input from the
keyboard and displayed their output on the screen
• However, the input data can be large that it will be
inconvenient to enter the input from the keyboard
– Example: processing large number of employees data
• Similarly, there are applications where the output will
be more useful if it is stored in a file
• The good news is that C allows the programmer to use
data files, both for input and output

CSE 115 Programming Language I ECE@NSU


20

Using Data Files


• The process of using data files for input/output
involves four steps as follows:
1. Declare pointer variables of type FILE *

2. Open the files for reading/writing using fopen function

3. Read/write the files using fscanf and fprintf

4. Close the files after processing the data using fclose

• In what follows, we explain each of these steps

CSE 115 Programming Language I ECE@NSU


21

Declaring FILE Pointer Variables


• Declare pointer variables to files as follows:
FILE *inp; /* pointer to input file */

FILE *outp; /* pointer to output file */

• Note that the type FILE is in upper case


– The type FILE stores information about an open file

• Also note the use of * before a pointer variable


– inp and outp are pointer variables
– Recall that pointer variables store memory addresses
CSE 115 Programming Language I ECE@NSU
22

Opening Data Files for I/O


• The second step is to open a file for reading or writing
• Suppose our input data exists in file: "data.txt"
• To open a file for reading, write the following:
inp = fopen("data.txt", "r");
• The "r" indicates the purpose of reading from a file
• Suppose we want to output data to: "results.txt"
• To open a file for writing, write the following:
outp = fopen("results.txt", "w");
• The "w" indicates the purpose of writing to a file
CSE 115 Programming Language I ECE@NSU
23

Handling File not Found Error


• inp = fopen("data.txt", "r");
• If the above fopen operation succeeds:
– It returns the address of the open FILE in inp
– The inp pointer can be used in all file read operations
• If the above fopen operation fails:
– For example, if the file data.txt is not found on disk
– It returns the NULL pointer value and assign it to inp
• Check the pointer inp immediately after fopen
if (inp == NULL)
printf("Cannot open file: data.txt\n");

CSE 115 Programming Language I ECE@NSU


24

Creating a File for Writing


• outp = fopen("results.txt", "w");
• If the above fopen operation succeeds:
– It returns the address of the open FILE in outp
– The outp pointer can be used in all file write operations
• If file results.txt does not exist on the disk
– The OS typically creates a new file results.txt on disk
• If file results.txt already exists on the disk
– The OS typically clears its content to make it a new file
• If fopen fails to create a new file for writing, it
returns the NULL pointer in outp
CSE 115 Programming Language I ECE@NSU
25

Input from & Output to Data Files


• The third step is to scan data from an input file and to
print results into an output file
• To input a double value from file data.txt, use:
fscanf(inp, "%lf", &data);
• The fscanf function works the same way as scanf
– Except that its first argument is an input FILE pointer
• To output a double value to results.txt, use:
fprintf(outp, "%f", data);
• Again, fprintf works similar to printf
– Except that its first argument is an output FILE pointer
CSE 115 Programming Language I ECE@NSU
26

Closing Input and Output Files


• The final step in using data files is to close the files
after you finish using them
• The fclose function is used to close both input and
output files as shown below:
fclose(inp);
fclose(outp);
• Warning: Do not forget to close files, especially
output files. This is necessary if you want to re-open a
file for reading after writing data to it. The OS might
delay writing data to a file until closed.
CSE 115 Programming Language I ECE@NSU
27

CSE 115 Programming Language I ECE@NSU


28

Sample Run
• File: indata.txt
344 55 6.3556 9.4
43.123 47.596
• File: outdata.txt
344.00
55.00
6.36
9.40
43.12
47.60

CSE 115 Programming Language I ECE@NSU


29

End-of-File Controlled Loop


• When reading input from a data file, the program does
not know how many data items to read
• Example: finding class average from student grades
• The grades are read from an input file one at a time in
a loop, until the end of file is reached
• The question here is how to detect the end of file?
• The good news is that fscanf returns a special value,
named EOF, when it encounters End-Of-File
• We can take advantage of this by using EOF as a
condition to control the termination of a loop
CSE 115 Programming Language I ECE@NSU
/* This program computes the average score of a class 30

The scores are read from an input file, scores.txt */


#include <stdio.h>
int main (void) {
FILE *infile;
double score, sum=0, average;
int count=0, status;
infile = fopen("scores.txt", "r");
status = fscanf(infile, "%lf", &score);
while (status != EOF) {
printf("%5.1f\n", score);
sum += score;
count++;
status = fscanf(infile, "%lf", &score);
}
average = sum / count;
printf("\nSum of scores is %.1f\n", sum);
printf("Average score is %.2f\n", average);
fclose(infile);
return 0;
}

CSE 115 Programming Language I ECE@NSU


31

Common Programming Errors


• Be careful when using pointer variables
– A pointer should be initialized to a valid address before use
– De-referencing an invalid/NULL pointer is a runtime error
• Calling functions with output parameters
– Remember that output parameters are pointers
– Pass the address of a variable to a pointer parameter
• Do not reference names outside their scope
• Create a file before reading it in a program
– Remember that fopen prepares a file for input/output
– The result of fopen should not be a NULL pointer
– Check the status of fscanf to ensure correct input
– Remember to use fclose to close a file, when done
CSE 115 Programming Language I ECE@NSU

You might also like