Pointers 2
Pointers 2
Part 2
POINTERS
• Pointers play an important role when used as parameters in
function calls
• The following is an old example:
1. The function swap() uses pointer to integer arguments, int *ptra and int
*ptrb
2. The main() function calls swap(&a, &b)
3. It passes the addresses of the ints it wishes to swap
Trace the swap function
a 1 b 2
a 1 b 2
main ()
swap()
t
Trace the swap function
a 1 b 2
main ()
swap()
t 1
Trace the swap function
a 1 b 2
main ()
swap()
t 1
Trace the swap function
a 1 b 2
main ()
swap()
t 1
TRY: Other methods to swap
two numbers
1. XOR operator (without using temporary variable)
2. Using #define macro
3. Using Arithmetic operators (+ -) without using
temporary variable.
Solution
1. XOR operator (without using temporary variable)
py is a pointer to char
that contains the address
of y
Pointer to a Pointer
• If we have a pointer P to some memory cell, P is also stored
somewhere in the memory
• So we can also talk about address of block that stores P.
int x=53;
45F 53 x
D
int x=53;
int *px=&x;
B4F 45F px 45F 53 x
7 D D
int x=53;
int *px=&x;
int **ppx=&px;