Chapter 6: Pointers

Example 6.1, Page Number: 107

In [1]:
from ctypes import *

balance =c_int(3200)       #int variable
balptr=pointer(balance)    #pointer to int
value=balptr[0]            #accessing the value using the pointer

#Result
print "balance: ",value    
balance:  3200

Example 6.2, Page Number: 109

In [2]:
#Variable declaration
x=123.23
y=c_double()
p=POINTER(c_int)

p=pointer(c_int(int(x)))    #type case double to int
y=p[0]

#Result
print y
123

Example 6.3, Page Number: 110

In [2]:
from ctypes import *

#Variable declaration
num=c_int()            #declaring int
p=pointer(num)         #pointer to int

p[0]=100
print num.value,
p[0]+=1
print num.value,
p[0]-=1
print num.value
100 101 100

Example 6.4, Page Number: 111

In [10]:
from ctypes import *

#Variable declaration
j=c_int()
g=c_double()
i=pointer(j)
f=pointer(g)


for x in range(10):
    print addressof(i.contents)+x,addressof(f.contents)+x
    
    
84638480 84639248
84638481 84639249
84638482 84639250
84638483 84639251
84638484 84639252
84638485 84639253
84638486 84639254
84638487 84639255
84638488 84639256
84638489 84639257

Example 6.5, Page Number: 114

In [12]:
from ctypes import *

#Variable declaration
str="This is a test"
token =""
i=0

#Read a token at a time from the string
while i<len(str):
    token=c_char_p("")    #set token to null string
    q=pointer(token)           
    #Read characters until either a space or the null terminator is encountered'''
    while i<len(str) and not(str[i]==" "):
        q[0]+=str[i]
        i+=1
    i+=1                 #advance past the space
    print q[0]
This
is
a
test

Example 6.6, Page Number: 115

In [14]:
#Variable declaration
str="This is a test"
i=0

#Read a token at a time from the string
while i<len(str):
    token=""           #set q to null string
    #Read characters until either a space or the null terminator is encountered'''
    while i<len(str) and not(str[i]==" "):
        token+=str[i]
        i+=1
    i+=1           #advance past the space
    print token
This
is
a
test

Example 6.7, Page Number: 115

In [15]:
import string

str=c_char_p("hello tom")
q=""
p=pointer(str)              #put address of str into p
   
p[0]=string.upper(p[0])

#Result
print p[0]
HELLO TOM

Example 6.8, Page Number: 117

In [4]:
#Varible declaration
s="Pointers are fun to use"

#Result
print s
Pointers are fun to use

Example 6.9, Page Number: 118

In [5]:
from ctypes import *

#Variable declaration
num=[]

#User input
for i in range(10):
    num.append(i)
    
start=num          #set start to the starting pointer 
for i in range(10):
    print start[i],
    
0 1 2 3 4 5 6 7 8 9

Example 6.10, Page Number: 119

In [6]:
import random

fortunes=["Soon, you will come into some money.",
          "A new love will enter your lifr. ",
          "You will live long and prosper.",
          "Now is a good time to invest for the future.",
          "A close friend will ask for a favour."]

print "To see your fortune, press a key: "

#Randomize the random number generator
chance=random.randint(0,100)
chance=chance%5

#Result
print fortunes[chance]
To see your fortune, press a key: 
A close friend will ask for a favour.

Example 6.11, Page Number: 120

In [7]:
keyword=[["for","for(initialization; condition; increment"],
         ["if","if(condition) ... else ..."],
         ["switch","switch(value) { case-list }"],
         ["while","while(condition) ..."],
         ["",""]]     #Terminates the list with nulls
          
#User input      
print "Enter keyword: "
str="for"              

for i in range(4):
    if str==keyword[i][0]:
        print keyword[i][1]
        
Enter keyword: 
for(initialization; condition; increment

Example 6.12, Page Number: 123

In [3]:
from ctypes import *

#Variable declaration
x=c_int(10)          #int variable
p=pointer(x)         #pointer to int
q=pointer(p)         #pointer to a pointer

#Result
print q[0][0]        #accessing the value using a pointer to a pointer
10

Example 6.13, Page Number: 126

In [9]:
from ctypes import *

#Variable declaration
s=c_char_p()
p1=pointer(s)
x=0

while True:
    print "\nEnter a string: ",
    if x==2:
        p1[0]=c_char_p("done")
    else:
        p1[0]=c_char_p("Hello")
    #print the ASCII values of each characcter
    for i in range(0,len(p1[0])):
        print ord(p1[0][i]),
    x+=1
    if p1[0]=="done":
        break
        
Enter a string:  72 101 108 108 111 
Enter a string:  72 101 108 108 111 
Enter a string:  100 111 110 101
In [ ]: