You are on page 1of 32

Unix Systems Programming

(CSE 3041)

Pointers and Modular programming


Chapter-6

SOA
Deemded to be University

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 1 / 32


Pointers

e rs
int
Po

Poin
t ers

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 2 / 32


Pointer notations
Consider the declaration:
int i = 3 ;

This declaration tells the C compiler to:


a. Reserve space in memory to hold the integer value.
b. Associate the name i with this memory location.
c. Store the value 3 at this location.

Figure: variable i location in memory.

Important: i’s address in memory is a number.


(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 3 / 32
Address of operator: &

Problem
WAP to print the address number of a variable.

Program

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 4 / 32


‘value at address’ operator: *

This version of ”*” is a Unary operator.


It gives the value stored at a particular address.
Also called unary indirection operator.

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 5 / 32


Pointers

&i: address of the variable i.


collect it in another variable.

j = &i ;

j is not an ordinary variable.


It is a variable that contains the address of other variable.
since it is a variable, it is allocated space in memory by the compiler.

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 6 / 32


What about the declaration of variable i?

since j is a variable that contains the address of i, it is declared as,


Syntax
int *j ;

Tells the compiler: j will be used to store the address of an integer


value.
In other words j points to an integer.
int *j would mean, the value at the address contained in j is an int.

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 7 / 32


Type this code and execute

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 8 / 32


Pointers

Pointers to different types


/* alpha is going to contain the address of an int value */
int *alpha ;
/* ch is going to contain the address of a char value */
char *ch ;
/* s is going to contain the address of a floating-point value*/
float *s ;

alpha, ch and s are declared as pointer variables.


They are variables capable of holding addresses.
Pointers are variables that contain addresses.
Pointers would always contain whole numbers.

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 9 / 32


Extending the basic concept of pointers
Pointers
Pointer is a variable that contains address of another variable.
A memory cell that stores the address of a data item.

Figure: Can we use more indirection ?

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 10 / 32


Figure: Can we use more indirection ?

if a pointer to a variableis defined as int *j , How should be define a


pointer to a pointer ?
In what different ways the address of i can be accessed ?
In what different ways the address of j can be accessed ?
In what different ways the address of k can be accessed ?
How can the value of j = address of i can be accessed ?
How can the value of k = address of j can be accessed ?
In what different ways the value of i can be accessed ?

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 11 / 32


Type the program and observe the results

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 12 / 32


APPLICATIONS OF POINTERS

Functions with Output Parameters


Multiple Calls to a Function with Input/Output Parameters
Formal Output Parameters as Actual Arguments
Pointers to Files

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 13 / 32


Back to Functions

C program is a collection of one or more functions.


A function gets called when the function name is followed by a semi-
colon.
A function is defined when function name is followed by a pair of braces
in which one or more statements may be present.
Any function can be called from any other function. Even main( ) can
be called from other functions.
A function can be called any number of times.
The order in which the functions are defined in a program and the order
in which they get called need not necessarily be same.
A function can call itself. Such a process is called ‘recursion’.
A function can be called from other function, but a function cannot be
defined in another function.
Two type of functions: built-in and user-defined.
(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 14 / 32
Functions with Output Parameters

Already, we have seen examples of


Functions without arguments
Functions with one input argument and no return value
Functions with one input argument and one return value
Functions with multiple input arguments.
Recall that :
Input arguments are arguments used to pass information into a function
subprogram.
Output arguments are used to return results to the calling function.
Objective
To be able to use output parameters to return multiple results
from a function.

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 15 / 32


Functions with Output Parameters

Problem statement
Write a program to separates a number into three parts: a sign
(+, -, or blank), a whole number magnitude, and a fractional part.

Suppose the number is stored in variable num.

Get sign using : /* Finds magnitude of num (its


if (num < 0) absolute value)
sign = ’-’; magnitude = fabs(num);
else if (num == 0) /*Separates it into whole and
sign = ’ ’; fractional parts */
else whole = floor(magnitude);
sign = ’+’; frac = magnitude - whole;

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 16 / 32


Practice problem

Figure: Memory Map for main() and sub()

Fill in the chart by indicating the data type and value of each reference as well as
the name of the function in which the reference would be legal.
(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 17 / 32
Multiple Calls to a Function with Input/Output
Parameters

Write a function subprogram named order that swaps its input param-
eters (two in number) only if the first one is larger than the second.

Write a program to sort three numbers. Use the function written above
(named, order ) to swap its inputs if the first input parameter is greater
than the second input parameter.
Multiple calls to function subprogram order may be required to sort the
inputs.

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 18 / 32


(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 19 / 32
Scope of a variable

The scope of a name refers to the region of a program where a particular


meaning of a name is visible or can be referenced.

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 20 / 32


Formal Output Parameters as Actual Arguments
The possibilities for actual arguments in calls to functions can be
local variables
input parameters of the calling function, and
output parameter of the calling function .
Simple example
WAP to find sum of two numbers. Write a function subprogram
for each of the following steps:
1) Read inputs. 2) Perform addition. 3) Print result.

Problem Statement
Write a program that scans a data line representing a common
fraction of the form
numerator / denominator
where numerator is an integer and denominator is a positive inte-
ger. The / symbol is a separator.

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 21 / 32


Passing an Argument x to Function some fun

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 22 / 32


Pointers to Files

Use input/output redirection to attempt


Reading each number from an input file (indata.txt) and writing it
rounded to 2 decimal places on a line of an output file (outdata.txt).

C allows a program to explicitly name a file (using pointers) from which


the program will take input or write output.
Declare pointer variables of type FILE *

OS prepares a file for read/write before granting access

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 23 / 32


Pointers to files

Using the functions fscanf and fprintf for files:

Close input and output files using fclose with the file pointers.

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 24 / 32


Simplified Case Study-I

Write a program that processes three files to print Roll number of the topper
student in a class along with marks she/he scored in USP subject. You are
given 3 files as below:

roll file.txt toppers file.txt USP file.txt


202101001 7 41.5
202101002 4 74
202101003 5 69.5
202101004 3 81
202101005 8 40.5
202101006 1 94
202101007 2 88.5
202101008 6 54

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 25 / 32


Case Study 2: Arithmetic for Common Fractions

Write an interactive menu driven program to add, subtract, multiply,


and divide several pairs of common fractions.
You should display your results as integer ratios; therefore,
I you need to be able to perform computations with common fractions
and
I get results that are common fractions in reduced form.

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 26 / 32


Analysis

Problem statement specifies that results are to be in reduced form, we will


need to include a fraction-reducing function in addition to the computational
functions.

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 27 / 32


Design

Step 3.2 Refinement


3.2.1 Find the greatest common divisor (gcd)
of the numerator and denominator
3.2.2 Divide the numerator and
denominator by the gcd

Initial algorithm and its refinement.

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 28 / 32


Design: structure chart

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 29 / 32


STUBS

It a skeleton function that consists of a header and statements that


display trace messages and assign values to output parameters.
It enables testing of the flow of control among functions before this
function is completed.
Such Skeleton functions allow testing of the partial system.

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 30 / 32


Debugging and Testing a Program System
Top-down testing is the process of testing flow of control between a main
function and its subordinate functions.
When a team of programmers is working on a problem, not all functions
will be ready at the same time.
STUBS are skeleton functions that were not yet written.
enables us to test and debug the main program flow and those functions
that are available.
Each stub displays an identification message and assigns values to its
output parameters to prevent execution errors caused by undefined val-
ues.
If a program contains one or more stubs,
I the message printed by each stub when it is called provides a trace of
the call sequence and
I allows the programmer to determine whether the flow of control within
the program is correct.
(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 31 / 32
Suggested Readings: Chapter 6 of prescribed Textbook and attempt all
programming problems given within and at the end of the Chapter.

(Unix Systems Programming) Chapter-6, pointers, C programming November 21, 2021 32 / 32

You might also like