Chapter 14 : Accessing Variable through Pointers

Example: 1, Page Number: 5.107

In [9]:
#Program to accessing through variable pointer

from ctypes import c_int

#Local definition
a = 22
a = c_int(a) # ctype datatype declaration

b = 2.25
b = c_float(b) # ctype datatype declaration

#Pointer variables
a_po=pointer(a)
b_po=pointer(b)

# 'id' is a address of letter and it represents the address of the variable
a = id(a)
b = id(b)


#Result
print "\nValue of a = %d" %a_po[0]
print "\nValue of b = %.2f" %b_po[0]
Value of a = 22

Value of b = 2.25

Example: 2, Page Number: 5.108

In [8]:
#Program to print address and value of variable

from ctypes import c_int

#Local definition
a = 22
a = c_int(a) # ctype datatype declaration

#Pointer variables
a_po=pointer(a)

# 'id' is a address of letter and it represents the address of the variable
a = id(a)

#Result
print "\n Value of a = %d" %a_po[0]
print "\n Address of a = %u" %id(a)
print "\n Value at address %u = %d" %(id(a),a_po[0])
 Value of a = 22

 Address of a = 4344066728

 Value at address 4344066728 = 22

Example: 3, Page Number: 5.108

In [10]:
# Program to print value and address of variable

from ctypes import c_int

#Local definition
a = 22
b = c_int(a) # ctype datatype declaration

#Pointer variable
b_po=pointer(b)

# 'id' is a address of letter and it represents the address of the variable
b = id(a)

#Result
print "\n Value of a = %d" %a
print "\n Value of a = %d" %(c_int(a).value)
print "\n Value of a = %d" %b_po[0]
print "\n Address of a = %u" %id(a)
print "\n Address of a = %u" %b
print "\n Address of a = %u" %id(b)
print "\n Value of b = address of a = %u" %b
 Value of a = 22

 Value of a = 22

 Value of a = 22

 Address of a = 4298180848

 Address of a = 4298180848

 Address of a = 4344066728

 Value of b = address of a = 4298180848

Example: 4, Page Number: 5.109

In [14]:
#Program to illustrate pointer to pointer

from ctypes import c_int

#Local definition
a = 22
b = c_int(a) # ctype datatype declaration
c = c_int(a) # ctype datatype declaration

#Pointer variable
b_po = pointer(b)
c_po = pointer(b_po) # c_po is a pointer to another pointer

# 'id' is a address of letter and it represents the address of the variable
b = id(a)
c = id(b)

#Result
print "\n Value of a is %d" %a
print "\n Value of a is %d" %(c_int(a).value)
 Value of a is 22

 Value of a is 22