# Capter 7: Pointers and References¶

## Example 7.1, Page no: 157¶

In [1]:
n=44
print "n = " , n
# prints the value of n
print "&n = " , hex(id(n)) # prints the address of n

n =  44
&n =  0x8fc0eec


## Example 7.2, Page no: 157¶

In [2]:
'''
Note : Python doesn't support reference/pointer variable. But can be achieved by using mutable datatypes i.e. list.
'''

n = [44]
rn=n # r is a synonym for n
print "n = " , n , ", rn = " , rn
n[0] -= 1
print "n = " , n , ", rn = " , rn
rn[0] *= 2
print "n = " , n , ", rn = " , rn

n =  [44] , rn =  [44]
n =  [43] , rn =  [43]
n =  [86] , rn =  [86]


## Example 7.3, Page no: 158¶

In [3]:
n = [44]
rn=n # r is a synonym for n
print "&n = " , hex(id(n)) , ", rn = " , hex(id(rn ))
rn2 = n
rn3 = rn
print "&rn2 = " , hex(id(rn2)) , ", rn = " , hex(id(rn ))

&n =  0x9c6228c , rn =  0x9c6228c
&rn2 =  0x9c6228c , rn =  0x9c6228c


## Example 7.4, Page no: 159¶

In [4]:
n = [44]
print "n = " , n , ", &n = " , hex(id(n))
pn = n
print "pn = " , hex(id(pn)) , ", &pn = " , hex(id(hex(id(pn))))

n =  [44] , &n =  0x9c624ec
pn =  0x9c624ec , &pn =  0x9c6aa60


## Example 7.5, Page no: 159¶

In [5]:
n = [44]
print "n = " , n , ", &n = " , hex(id(n))
pn = n
print "\tpn = " , hex(id(pn)) , ",\n &pn = " , hex(id(hex(id(pn))))
print "*pn = " , pn

n =  [44] , &n =  0x9c58d6c
pn =  0x9c58d6c ,
&pn =  0x9c6ab20
*pn =  [44]


## Example 7.6, Page no: 160¶

In [6]:
n = [44]
print "n = " , n , ", &n = " , hex(id(n))
pn = n
print "\tpn = " , hex(id(pn)) , ",\n &pn = " , hex(id(hex(id(pn))))
print "*pn = " , pn
ppn = pn

print " ppn = " , hex(id(hex(id(ppn))))
print " &ppn = " , hex(id(hex(id(hex(id(ppn))))))
print " *ppn = " , hex(id(ppn))
print "**ppn = " , ppn

n =  [44] , &n =  0x9bf05ac
pn =  0x9bf05ac ,
&pn =  0x9c58160
*pn =  [44]
ppn =  0x9c58680
&ppn =  0x9c58160
*ppn =  0x9bf05ac
**ppn =  [44]


## Example 7.7, Page no: 160¶

In [7]:
n = [44]
print "n = " , n , "\n &n = " , hex(id(n))
pn = n
print "\tpn = " , hex(id(pn)) , ",\n &pn = " , hex(id(hex(id(pn))))
print "*pn = " , pn
nn = pn
print " ppn = " , hex(id(nn))
print " &ppn = " , hex(id(hex(id(nn))))
rpn = pn
print " ppn = " , hex(id(rpn))
print " &ppn = " , hex(id(hex(id(rpn))))

n =  [44]
&n =  0x9bf60ec
pn =  0x9bf60ec ,
&pn =  0x9bf0e40
*pn =  [44]
ppn =  0x9bf60ec
&ppn =  0x9bf0e40
ppn =  0x9bf60ec
&ppn =  0x9bf0f20


## Example 7.8, Page no: 162¶

In [8]:
def max_(m,n):
if m>n:
return m
else:
return n

m = 44
n = 22
print m , ", " , n , ", " , max_(m,n)
m = max_(m,n)
m = 55
# changes the value of m from 44 to 55
print m , ", " , n , ", " , max_(m,n)

44 ,  22 ,  44
55 ,  22 ,  55


## Example 7.9, Page no: 163¶

In [9]:
v = []
for k in range(1,5):
v.append(1.0/k)

for i in range(4):
print "v[" , i , "] = " , v[i]

v[ 0 ] =  1.0
v[ 1 ] =  0.5
v[ 2 ] =  0.333333333333
v[ 3 ] =  0.25


## Example 7.10, Page no: 163¶

In [10]:
import sys
a = [22, 33, 44]

print  "a = " , hex(id(a))
print "sizeof(int) = " , sys.getsizeof(1)
s = 0
for i in a:
s += i
print "\t i = " , hex(id(i)),
print "\t *i = " , i,
print "\t sum = " , s

a =  0x9bf688c
sizeof(int) =  12
i =  0x8fc0ff4 	 *i =  22 	 sum =  22
i =  0x8fc0f70 	 *i =  33 	 sum =  55
i =  0x8fc0eec 	 *i =  44 	 sum =  99


## Example 7.11, Page no: 165¶

In [11]:
a = [22, 33, 44, 55, 66]
print "a = " , hex(id(a)) , ", *a = " , a[0]
for p in a:
print "p = " , hex(id(p)) , ", *p = " , p

a =  0x9c6526c , *a =  22
p =  0x8fc0ff4 , *p =  22
p =  0x8fc0f70 , *p =  33
p =  0x8fc0eec , *p =  44
p =  0x8fc0e68 , *p =  55
p =  0x8fc0de4 , *p =  66


## Example 7.12, Page no: 165¶

In [12]:
def loc(a1,a2,n1,n2):
p = []
for element in a2:
if element in a1:
p.append(element)
return p

a1 = [11, 11, 11, 11, 11, 22, 33, 44, 55]
a2 = [11, 11, 11, 22, 33]
print "Array a1 begins at location\t" , hex(id(a1 ))
print "Array a2 begins at location\t" , hex(id(a2))
p = loc(a1, a2, 9, 5)
if (p):
print "Array a2 found at location\t" , hex(id(p))
for i in range(len(p)):
print "\t" , hex(id(p[i])) , ": " , p[i], "\t" , hex(id(a2[i])) , ": " , a2[i]
else:

Array a1 begins at location	0x9bea56c
Array a2 begins at location	0x9bea62c
Array a2 found at location	0x9bea6cc
0x8fc1078 :  11 	0x8fc1078 :  11
0x8fc1078 :  11 	0x8fc1078 :  11
0x8fc1078 :  11 	0x8fc1078 :  11
0x8fc0ff4 :  22 	0x8fc0ff4 :  22
0x8fc0f70 :  33 	0x8fc0f70 :  33


## Example 7.13, Page no: 166¶

In [13]:
def get(a):
print "Enter number of items: "
n = int(raw_input())
print "Enter " , n , " items, one per line:"
for i in range(n):
print "\t" , i+1 , ": ",
a.append(float(raw_input()))

def print_(a):
for i in range(len(a)):
print a[i] ,
print ''

a = []
get(a)
print_(a)
a = []
get(a)
print_(a)

Enter number of items:
4
Enter  4  items, one per line:
1 : 44.4
2 : 77.7
3 : 22.2
4 : 88.8
44.4 77.7 22.2 88.8
Enter number of items:
2
Enter  2  items, one per line:
1 : 3.33
2 : 9.99
3.33 9.99


## Example 7.14, Page no: 167¶

In [14]:
def sort(p, n):
for i in range(1,n):
for j in range(n-i):
if (p[j] > p[j+1]):
p[j],p[j+1] = p[j+1],p[j]


## Example 7.15, Page no: 168¶

In [15]:
def sum_(k,n):
# returns the sum f(0) + f(1) + f(2) + . . . + f(n-1):
s = 0
for i in range(1,n+1):
s += k(i)
return s

def square(k):
return k*k

def cube(k):
return k*k*k

print sum_(square,4) # 1 + 4 + 9 + 16
print sum_(cube,4)  # 1 + 8 + 27 + 64

30
100