Title: EE441 Data Structures Chapter IV Pointers, Argument Passing
1EE441 Data StructuresChapter IVPointers,
Argument Passing
- Cüneyt F. Bazlamaçci
- Department of Electrical Electronics
Engineering - Middle East Technical University
- cuneytb_at_metu.edu.tr
- www.eee.metu.edu.tr/cb
2Pointers
- A pointer variable holds a value that is the
address of an object in memory. - Through a pointer an object can be referenced
directly. - Typical uses of pointers are the creation of
linked lists and the management of objects
allocated during execution. -
- int ip // pointer decleration
- int ip // the same as above
- int ip1, ip2 // two pointers
- int ip1, ip2 // a pointer, an integer
3Pointers
- A pointer is a variable that holds the address
of something else.
int foo int x foo 123 x foo
foo
123
x
3
4Pointers (Examples)
- e.g.
- int i1, i2
- here, i1 will contain a pointer to an integer
- i2 will contain an integer
- Similarly,
- float f1, f2, f3
- f1 and f2 are pointers to floating point
variables - f3 is itself a floating point variable
- Some more
- int ip, ip2 // an integer and an integer
pointer - long lp, lp2 // a long integer pointer and a
long integer - float fp, fp2 // a floating point and a
floating point pointer
5Address-of
- In C you can get the address of a variable with
the operator.
int foo foo 123 x foo
foo
123
foo means the address of foo
6Assigning Value to Pointers
- A pointer must have a value before you can
dereference it (follow the pointer).
int x x3
int foo int x x foo x3
ERROR!!! x doesnt point to anything!!!
this is fine x points to foo
7Pointers (Examples)
- e.g.
- int i856
- int pi // error! type mismatch.
- int pi // creates a pointer p that points
to i // address of integer i is stored in
pointer p - int p2p // a second pointer-to-integer
called p2 - // is created and the address in p is copied
// into p2 - int kp // error! type mismatch
- int kp // value 856 is copied into k
- p2k // copy address of k into pointer p2
- p2i // copy value of i into address pointed
by p2 - // i.e., value of k now becomes 856
8Pointers (Examples)
- e.g.
- pp2 // add 2 to the value in the address
pointed - // by p (i.e., i now becomes 858)
- pp2 // increment the address in p by 2
- // (p no longer points to i)
- Hence
- p object pointed to by p
- p address of p
- p the pointer itself
9Pointers (Examples)
- e.g.
- i10 // copy the value 10 into the object
// pointed by the address of the variable i - i is not a variable, but if we declare
- int p
- p has type pointer-to-int which is of the same
type as i
10Pointers to anything
x
some int
some int
some int
y
z
some double
11Pointers and Arrays
- An array name is basically a const pointer.
- You can use the operator with a pointer
- int x
- int a10
- x a2
- for (int i0ilt3i)
- xi
x is the address of a2
xi is the same as ai2
12Information Passing Between Functions (Argument
Passing)
- The output or return value of a function may
(be of any type) belong to any class. - e.g.
- RandomNumber fr(int n)
- // a function that returns an object fr that
belongs to // class RandomNumber input parameter
n can // have any use, to be defined within
the function - The input parameters (or arguments) of a
function may be passed BY VALUE or BY REFERENCE.
13Information Passing Between Functions (Argument
Passing)
- BY VALUE The calling program copies the actual
full object value to the local data area of the
called program. - BY REFERENCE The address of the object as stored
by the calling program is passed into the called
program which operates on the original data not
its local copy!!!
14Argument Passing (Example)
- Suppose that we desire to swap the contents of
two integer variables. Consider the following
function
void swap(int v1, int v2) int tempv2
v2v1 v1temp
int main() int i10 int j20 cout ltlt
"Before swap()\ti"ltltiltlt"\tj"ltltjltltendl
swap(i,j) cout ltlt "After swap()\ti"ltltiltlt"\
tj"ltltjltltendl
15Argument Passing (Example)
- When this program is executed the result is not
as we desired, the output is - Before swap() i10 j20
- After swap() i10 j20
- Why?
16Argument Passing (Example)
- Before the variables inside the function have not
been passed out, copies were swapped - i.e., the output parameters in the argument list
must be passed by reference. - There are two alternatives two solve this
problem - Use pointers as parameters
- Use reference as parameters
17Pointers as Parameters
- Alternative 1 Pass pointers to the variables
void pswap(int v1, int v2) // input parameters
shall be pointers int tempv2 v2v1
v1temp void main() pswap(i,j)
// caller passes address of i and j
18Pointers as Parameters
- When executed we obtain
- Before swap() i10 j20
- After swap() i20 j10
- If we change what the pointer points to, the
caller will see the change!!
19Call by Reference
- Alternative 2 Another syntax to pass pointers
// input parameters are variables but compiler
should copy addrs. only void rswap(int v1, int
v2) int tempv2 v2v1 v1temp
void main() rswap(i,j) // caller passes
variables themselves, // compiler converts
this to copying addresses
When executed, correct results will be obtained!!!
20Find these example codes _at_
- The first try (i.e., with integer variables
leading to wrong result) - http//www.eee.metu.edu.tr/akan/EE441/swap.cpp
- Pointers as parameters (Alternative 1)
- http//www.eee.metu.edu.tr/akan/EE441/pswap.cpp
- Call by reference (Alternative 2)
- http//www.eee.metu.edu.tr/akan/EE441/rswap.cpp