Documente Academic
Documente Profesional
Documente Cultură
Umair Babar
http://lectures.pucit.edu.pk/studentViewsub.jsp?uid=232
umair.babar@pucit.edu.pk
Punjab University College of Information Technology.
P j b U i i C ll f I f i T h l
University of the Punjab, Lahore.
Pointers and Functions
y Pointers can be used to pass addresses of variables to
called functions, thus allowing the called function to
alter the values stored there.
y Previously implemented swap function did not change
the values stored in the main program because only
the values were passed to the function swap.
y This is known as "call by value".
Programming Fundamentals © Umair Babar. PUCIT, Lahore. 2
1
5/6/2009
Function Call by Value
#include <iostream.h>
int f(int x)
{
cout << "value of x = " << x << endl;
x = 4;
}
Output:
void main()
{
V l f
Value of x = 5
int v = 5; Value of v = 5
f(v);
cout << "value of v = " << v << endl;
}
Programming Fundamentals © Umair Babar. PUCIT, Lahore. 3
Function Call by Value
y When a variable v is passed by value to a function f, its
value is copied to the corresponding variable x in f.
y Call by value is the default mechanism for parameter passing
y p p g
in C/C++.
Programming Fundamentals © Umair Babar. PUCIT, Lahore. 4
2
5/6/2009
Pointers and Functions
y If instead of passing the values of the variables to the called
p g
function, we pass their addresses, so that the called function
can change the values stored in the calling routine.
y This is known as "call by reference" since we are referencing the
variables.
y The following shows the swap function modified from a "call
by value" to a "call by reference".
y Note that the values are now actually swapped when the
control is returned to main function.
Programming Fundamentals © Umair Babar. PUCIT, Lahore. 5
Pointers with Functions
#include <iostream.h>
void main()
{
int a = 5, b = 6;
3
5/6/2009
Pointers with Functions
void swap(int
p( *a,
, int *b)
)
{
int temp;
temp= *a;
*a= *b;
*b = temp ;
Function Call by Reference
#include <iostream.h>
4
5/6/2009
Function Call by Reference
y When a variable v is passed by reference to a parameter
x of function f, v and the corresponding.
Programming Fundamentals © Umair Babar. PUCIT, Lahore. 9
Function Call by Constant Reference
int f( const int &x )
{
cout << “value of x = “ << x << endl;
x = 4; // invalid
}
void main()
{
int v = 5;
f(v);
cout << “value of v = “ << v << endl;
}
Programming Fundamentals © Umair Babar. PUCIT, Lahore. 10
5
5/6/2009
Function Call by Constant Reference
y Passing variable
Passing variable v by
v by constant reference
constant reference to parameter
to parameter
x of f will NOT allow any change to the value of x.
Programming Fundamentals © Umair Babar. PUCIT, Lahore. 11
Usage of Parameter Passing
y Call by value
y is appropriate for small objects that should not be
changed by the function
y Call by constant reference
y is appropriate for large objects that should not be changed
by the function
y Call by reference
y is appropriate for all objects that may be changed by the
function
12
6
5/6/2009
Reference Variables
y Reference and constant reference variables are commonly used for
parameter passing to a function
y They can also be used as local variables.
y A reference (or constant reference) variable serves as an alternative
name for a variable
int m = 10;
int & j = m;
cout <<“value of m = “ <<m; //value of m printed is 10
j = 18;
cout << “value of m = “ <<m //value of m printed is 18
Programming Fundamentals © Umair Babar. PUCIT, Lahore. 13
Reference Variables
y A reference variable is different from a pointer.
p
y A pointer need NOT be initialized while defining, but a
reference variable should always refer to some other object
int * p;
int m = 10;
int & j = m; //valid
int & k; //compilation error
Programming Fundamentals © Umair Babar. PUCIT, Lahore. 14
7
5/6/2009
References (Summary)
y References are an additional name to an existing
memory location
y If we wanted something called “ref” to refer to a
variable x:
Pointer: Reference:
x 9 x
9
ref
ref
Programming Fundamentals © Umair Babar. PUCIT, Lahore. 15
Pointer vs. Reference
y A pointer can be assigned a new value to point at a
different object, but a reference variable always
refers to the same object.
y Assigning a reference variable with a new value
actually changes the value of the referred object.
actually changes the value of the referred object
Programming Fundamentals © Umair Babar. PUCIT, Lahore. 16
8
5/6/2009
Pointer vs. Reference
int * p;
int m = 10;
int & j = m; //valid
p = &m; //p now points at m
int n = 12;
j = n; //the value of m is set to 12.
//But j still refers to m, not to n.
cout << “value of m = “ << m <<endl;
//value of m printed is 12
n = 36;
cout << “value of j = “ << j << endl;
//value of j printed is 12
p = &n;
Programming Fundamentals © Umair Babar. PUCIT, Lahore. 17
Pointer vs. Reference
y A constant reference variable v refers to an object
whose value cannot be changed through v.
int m = 8;
const int & j = m;
m = 16; //valid
j = 20; //compilation error
Programming Fundamentals © Umair Babar. PUCIT, Lahore. 18
9
5/6/2009
Arithmetic and Logical
Operations on Pointers
y A pointer may be incremented or decremented
y An integer may be added to or subtracted from a
pointer.
y Pointer variables may be subtracted from one
another.
th
y Pointer variables can be used in comparisons, but
usually only in a comparison to NULL.
Programming Fundamentals © Umair Babar. PUCIT, Lahore. 19
Arithmetic Operations on Pointers
y When an integer is added to or subtracted from a
pointer, the new pointer value is changed by the
integer times the number of bytes in the data variable
the pointer is pointing to.
y For example, if the pointer valptr contains the address
of a double precision variable and that address is
234567870, then the statement:
valptr = valptr + 2;
would change valptr to 234567886
Programming Fundamentals © Umair Babar. PUCIT, Lahore. 20
10
5/6/2009
Using the C Language Special Keyword
y sizeof
y This keyword can be used to determine the number
of bytes in a data type, a variable, or an array
y Example:
double array [10];
y [ ];
sizeof (double); // Returns the value 8
sizeof (array); // Returns the value 80
sizeof(array)/sizeof(double); // Returns 10
Programming Fundamentals © Umair Babar. PUCIT, Lahore. 21
The End
Questions?
Programming Fundamentals © Umair Babar. PUCIT, Lahore. 22
11