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
#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
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
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
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]
#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
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]
#Varible declaration
s="Pointers are fun to use"
#Result
print s
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],
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]
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]
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
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