Hour 11: Understanding Pointers

Example 11.1, Page No 178

In [2]:
c=chr
x=int
y=float

print "Address="+str(id(c))+" Content="+str(c)
print "Address="+str(id(x))+" Content="+str(x)
print "Address="+str(id(y))+" Content="+str(y)

c='A'
x=7
y=123.45

print "Address="+str(id(c))+" Content="+str(c)
print "Address="+str(id(x))+" Content="+str(x)
print "Address="+str(id(y))+" Content="+str(y)
Address=19935400 Content=<built-in function chr>
Address=505552912 Content=<type 'int'>
Address=505547744 Content=<type 'float'>
Address=20542336 Content=A
Address=20177304 Content=7
Address=41743224 Content=123.45

Example 11.2, Page No 180

In [3]:
c=chr
x=int
y=float
prt_c=chr
ptr_x=int
ptr_y=float

c='A'
x=7
y=123.45

print "C: Address="+str(id(c))+" Content="+str(c)
print "X: Address="+str(id(x))+" Content="+str(x)
print "Y: Address="+str(id(y))+" Content="+str(y)

ptr_c=id(c)
print "ptr_c: Address="+str(id(ptr_c))+" Content="+str(ptr_c)
print "*ptr_c -> "+str(c)

ptr_x=id(x)
print "ptr_x: Address="+str(id(ptr_x))+" Content="+str(ptr_x)
print "*ptr_x -> "+str(x)

ptr_y=id(y)
print "ptr_y: Address="+str(id(ptr_y))+" Content="+str(ptr_y)
print "*ptr_y -> "+str(y)
C: Address=20542336 Content=A
X: Address=20177304 Content=7
Y: Address=41743208 Content=123.45
ptr_c: Address=42208720 Content=20542336
*ptr_c -> A
ptr_x: Address=42206796 Content=20177304
*ptr_x -> 7
ptr_y: Address=42206784 Content=41743208
*ptr_y -> 123.45

Example 11.3, Page No 183

In [4]:
c=chr
ptr_c=chr

c='A'
print "C: Address="+str(id(c))+" Content="+str(c)

ptr_c=id(c)
print "ptr_c: Address="+str(id(ptr_c))+" Content="+str(ptr_c)
print "*ptr_c -> "+str(c)

ptr_c='B'
print "ptr_c: Address="+str(id(ptr_c))+" Content="+str(ptr_c)
print "*ptr_c -> "+str(ptr_c)

print "C: Address="+str(id(c))+" Content="+str(c)
C: Address=20542336 Content=A
ptr_c: Address=42206808 Content=20542336
*ptr_c -> A
ptr_c: Address=20342424 Content=B
*ptr_c -> B
C: Address=20542336 Content=A

Example 11.4, Page No 185

In [5]:
x=1234
print "X: address="+str(id(x))+" Content="+str(x)

ptr_1=id(x)
print "ptr_1: Address="+str(id(ptr_1))+" Content="+str(ptr_1)
print "*ptr_1 -> "+str(ptr_1)

ptr_2=id(x)
print "ptr_2: Address="+str(id(ptr_2))+" Content="+str(ptr_2)
print "*ptr_2 -> "+str(ptr_2)

ptr_3=ptr_1
print "ptr_3: Address="+str(id(ptr_3))+" Content="+str(ptr_3)
print "*ptr_3 -> "+str(ptr_3)
X: address=42206832 Content=1234
ptr_1: Address=42206928 Content=42206832
*ptr_1 -> 42206832
ptr_2: Address=42206916 Content=42206832
*ptr_2 -> 42206832
ptr_3: Address=42206928 Content=42206832
*ptr_3 -> 42206832