Hour 16: Applying Pointers

Example 16.1, Page No 260

In [17]:
# There is no concept of pointer in pyton and this program is based on pointer so i have done in this manner
ptr_ch=chr
ptr_int=int
ptr_db=float

#Char pointer ptr_ch
print " Corrent position of ptr_ch: %#x " % id(ptr_ch)
print " The position after ptr_ch+1 : %#x" % (id(ptr_ch)+1)
print " The position after ptr_ch+2 : %#x" % (id(ptr_ch)+2)
print " The position after ptr_ch+1 : %#x" % (id(ptr_ch)+1)
print " The position after ptr_ch+2 : %#x\n" % (id(ptr_ch)+2)

print " Corrent position of ptr_int: %#x " % id(ptr_int)
print " The position after ptr_int+1 : %#x" % (id(ptr_int)+1)
print " The position after ptr_int+2 : %#x" % (id(ptr_int)+2)
print " The position after ptr_int+1 : %#x" % (id(ptr_int)+1)
print " The position after ptr_int+2 : %#x\n" % (id(ptr_int)+2)

print " Corrent position of ptr_db: %#x " % id(ptr_db)
print " The position after ptr_db+1 : %#x" % (id(ptr_db)+1)
print " The position after ptr_db+2 : %#x" % (id(ptr_db)+2)
print " The position after ptr_db+1 : %#x" % (id(ptr_db)+1)
print " The position after ptr_db+2 : %#x\n" % (id(ptr_db)+2)
 Corrent position of ptr_ch: 0x12a30a8 
 The position after ptr_ch+1 : 0x12a30a9
 The position after ptr_ch+2 : 0x12a30aa
 The position after ptr_ch+1 : 0x12a30a9
 The position after ptr_ch+2 : 0x12a30aa

 Corrent position of ptr_int: 0x1e222010 
 The position after ptr_int+1 : 0x1e222011
 The position after ptr_int+2 : 0x1e222012
 The position after ptr_int+1 : 0x1e222011
 The position after ptr_int+2 : 0x1e222012

 Corrent position of ptr_db: 0x1e220be0 
 The position after ptr_db+1 : 0x1e220be1
 The position after ptr_db+2 : 0x1e220be2
 The position after ptr_db+1 : 0x1e220be1
 The position after ptr_db+2 : 0x1e220be2

Example 16.2, Page No 263

In [24]:
# There is no concept of pointer in pyton and this program is based on pointer so i have done in this manner
ptr_int1=int
ptr_int2=int

print "The position of ptr_int1: %#x" % id(ptr_int1)
ptr_int2=id(ptr_int1)+5
print "The position of ptr_int2=id(ptr_int1)+5 :%#x" % ptr_int2
print "The subtraction of ptr_int2-ptr_int1 :%#x" % (ptr_int2 - id(ptr_int1))

ptr_int2=id(ptr_int1)-5
print "The position of ptr_int2=id(ptr_int1)-5 :%#x" % ptr_int2
print "The subtraction of ptr_int2-ptr_int1 :%#x" % (ptr_int2 - id(ptr_int1))
The position of ptr_int1: 0x1e222010
The position of ptr_int2=id(ptr_int1)+5 :0x1e222015
The subtraction of ptr_int2-ptr_int1 :0x5
The position of ptr_int2=id(ptr_int1)-5 :0x1e22200b
The subtraction of ptr_int2-ptr_int1 :-0x5

Example 16.3, Page No 265

In [32]:
# There is no concept of pointer in pyton and this program is based on pointer so i have done in this manner
str1="It's a string!"
ptr_str=chr
list1=[1,2,3,4,5]
ptr_int=int
str1=list(str1)
ptr_str=str1
print "Before the change, str1 contains: ",
print "".join(str1)
print "Before the change, str1[5] contains : %c" % str1[5]
str1[5]='A'
print "After The change, str1[5] contains : %c" % str1[5]
print "After The change, str1 contains: ",
print "".join(str1)

ptr_int=list1
print "Before The change, list1[2] contains: %d" % list1[2]
list1[2]=-3
print "After the change, list1[2] contains : %d" % list1[2]
Before the change, str1 contains:  It's a string!
Before the change, str1[5] contains : a
After The change, str1[5] contains : A
After The change, str1 contains:  It's A string!
Before The change, list1[2] contains: 3
After the change, list1[2] contains : -3

Example 16.4, Page No 266

In [42]:
def AddThree(list1):
    result=0
    for i in range(len(list1)):
        result=result+list1[i]
    return result

sum1=0
listt=[x for x in range(3)]

listt[0]=int(raw_input("Enter integer1 :"))
listt[1]=int(raw_input("Enter integer2 :"))
listt[2]=int(raw_input("Enter integer3 :"))
sum1=AddThree(listt)
print "The sum of three integers is: %d" % sum1
Enter integer1 :10
Enter integer2 :20
Enter integer3 :30
The sum of three integers is: 60

Example 16.5, Page No 268

In [47]:
# There is no concept of pointer in pyton and this program is based on pointer so i have done in this manner
def ChPrint(c):
    print "%s" % c
def DataAdd(list1,max1):
    sum1=0
    for i in range(max1):
        sum1 += list1[i]
    return sum1

str1="It's a string!"
ptr_str=chr
listt=[1,2,3,4,5]
ptr_int=int

ptr_str=str1
ChPrint(ptr_str)
ChPrint(str1)

ptr_int=listt

print "The sum returned by DataAdd() : %d" % DataAdd(ptr_int,5)
print "The sum returned by DataAdd() : %d" % DataAdd(listt,5)
    
It's a string!
It's a string!
The sum returned by DataAdd() : 15
The sum returned by DataAdd() : 15

Example 16.6, Page No 270

In [12]:
# There is no concept of pointer in pyton and this program is based on pointer so i have done in this manner
def DataAdd(list1,max1,max2):
    sum1=0
    for i in range(max1):
        for j in range(max2):
            sum1 = sum1 + list1[i][j]
    return sum1
def DataAdd2(list2,max1,max2):
    sum2=0
    for i in range(max1):
        for j in range(max2):
            sum2 = sum2 + list2[i][j]
    return sum2

listt=[[1,2],[3,4],[5,5],[4,3],[2,1]]
ptr_int=int
print "The sum returned by DataAdd() : %d" % DataAdd(listt,5,2)
ptr_int=listt
print "The sum returned by DataAdd() : %d" % DataAdd2(ptr_int,5,2)
The sum returned by DataAdd() : 30
The sum returned by DataAdd() : 30

Example 16.7, Page No 272

In [14]:
def StrPrint1(str1,size):
    for i in range(size):
        print "%s" % str1[i]
def StrPrint2(str2):
    print "%s" % str2
    
str1=["There's music in the sighing of a reed;","There's music in the gushing of a rill;","There's music in all things if men had ears;","There earth is but an echo of the spheres.\n"]
size=4

StrPrint1(str1,size)
for i in range(size):
    StrPrint2(str1[i])
There's music in the sighing of a reed;
There's music in the gushing of a rill;
There's music in all things if men had ears;
There earth is but an echo of the spheres.

There's music in the sighing of a reed;
There's music in the gushing of a rill;
There's music in all things if men had ears;
There earth is but an echo of the spheres.

Example 16.8, Page No 274

In [18]:
# There is no concept of pointer in pyton and this program is based on pointer so i have done in this manner
def StrPrint(str1):
    print "%s" % str1

str1="Pointing to a function"
ptr=StrPrint
if(not(ptr(str1))):
    print "Done."
Pointing to a function
Done.