Chapter 9: Pointers

Example 9.1, Page number: 282

In [1]:
#Display the address of the variable

import sys

#Variable Initialization
num = int(raw_input("Enter a Number = "))

#Result
sys.stdout.write("Value of num = %d\n"%(num))
sys.stdout.write("Address of num = %d\n"%(id(num)))
Enter a Number = 20
Value of num = 20
Address of num = 19554932

Example 9.2, Page number: 283

In [3]:
#Display the addresses of different variables together with their values.

import sys

#Variable Initialization
c = raw_input("Enter alphabet")
i = int(raw_input("Enter number"))
f = float(raw_input("Enter float"))

#Result
sys.stdout.write("\nAddress of (c) %c = %d"%(c,id(c)))
sys.stdout.write("\nAddress of (i) %d = %d"%(i,id(i)))
sys.stdout.write("\nAddress of (f) %.2f = %d"%(f,id(f)))
#Memory address differs in different systems
Enter alphabetC
Enter number20
Enter float2.5

Address of (c) C = 48769192
Address of (i) 20 = 19554932
Address of (f) 2.50 = 44777808

Example 9.3, Page number: 284

In [8]:
#Show pointer of any data type that occupies same space

import sys

#Result
#Garbage collector module is used for python memory management
sys.stdout.write("char %d byte & its pointer %d bytes\n"%(sys.getsizeof(chr),sys.getsizeof(id(chr))))
sys.stdout.write("int %d byte & its pointer %d bytes\n"%(sys.getsizeof(int),sys.getsizeof(id(int))))
sys.stdout.write("float %d byte & its pointer %d bytes\n"%(sys.getsizeof(float),sys.getsizeof(id(float))))

#value tagged method is used in python
char 36 byte & its pointer 12 bytes
int 436 byte & its pointer 12 bytes
float 436 byte & its pointer 12 bytes

Example 9.4, Page number: 284

In [9]:
#Display the value of variable and its location using pointer

import sys

#Variable Initialization
#There is no pointer concept in python
v = 10
p = v
#In python, variables of same value are tagged together instead of storing the value in different locations
#Result
sys.stdout.write("\nAddress of v = %u"%(id(v)))
sys.stdout.write("\nValue of v = %d"%(p))
sys.stdout.write("\nAddress of p = %d"%(id(p)))
Address of v = 29808764
Value of v = 10
Address of p = 29808764

Example 9.5, Page number: 285

In [4]:
#Print the value of variable by different ways.

import sys

#Variable Initialization
a = int(raw_input("Enter Integer Value"))
b = float(raw_input("Enter float Value"))

pa = id(a)
pb = id(b)

#Result
#There is no pointer concept in python. content of memory location can't be accessed directly by user.
sys.stdout.write("\nAddress of a = %u"%(pa))
sys.stdout.write("\nValue of a = %d"%(a))
sys.stdout.write("\nValue of a = %d"%(a))
sys.stdout.write("\nValue of a = %d"%(a))

sys.stdout.write("\nAddress of b = %u"%(pb))
sys.stdout.write("\nValue of b = %.2f"%(b))
sys.stdout.write("\nValue of b = %.2f"%(b))
sys.stdout.write("\nValue of b = %.2f"%(b))
Enter Integer Value4
Enter float Value2.2

Address of a = 19555124
Value of a = 4
Value of a = 4
Value of a = 4
Address of b = 44777792
Value of b = 2.20
Value of b = 2.20
Value of b = 2.20

Example 9.6, Page number: 286

In [13]:
#Print value of variable using different pointer notations.

import sys

#Variable Initialization
v = 10
p = id(v)

#Result
#There is no pointer concept in python and can't access memory location directly by user
sys.stdout.write("\nv = %d v = %d v = %d"%(v,v,v))
v = 10 v = 10 v = 10

Example 9.7, Page number: 287

In [5]:
#Print element and its address using pointer

import sys

#Variable Initialization
i = int(raw_input("Enter a number : "))
k = id(i)

#Result
#There is no pointer concept in python and can't access memory location directly by user
sys.stdout.write("\nAddress of i is %u"%(k))
sys.stdout.write("\nValue of i is %d"%(i))
#The memory address differs in different systems
Enter a number : 15

Address of i is 19554992
Value of i is 15

Example 9.8, Page number: 287

In [6]:
#Add two numbers through variables and their pointers

import sys

#Variable Initialization
a = int(raw_input("Enter Two Numbers : "))
b = int(raw_input("Enter Two Numbers : "))
ap = a
bp = b

#Calculation
c = a + b
d = ap + bp

#Result
#There is no pointer concept in python and can't access memory location directly by user
sys.stdout.write("\nSum of A & B Using Variable : %d"%(c))
sys.stdout.write("\nSum of A & B Using Pointer : %d"%(d))
Enter Two Numbers : 8
Enter Two Numbers : 4

Sum of A & B Using Variable : 12
Sum of A & B Using Pointer : 12

Example 9.9, Page number: 288

In [17]:
#Assign pointer value to another variable

import sys

#Variable Initialization
a = 5
c = id(a)
b = a

#Result
#There is no pointer concept in python and can't access memory location directly by user
sys.stdout.write("\nMemory location of 'a' = %u"%(c))
sys.stdout.write("\nThe value of a = %d & b = %d"%(a,b))
Memory location of 'a' = 29808824
The value of a = 5 & b = 5

Example 9.10, Page number: 289

In [7]:
#Assign value of 'b' to 'a' through pointers and add the values.

import sys

#Variable Initialization
a = int(raw_input("Enter Value of 'a' & 'b' : "))
b = int(raw_input("Enter Value of 'a' & 'b' : "))
pa = id(a)
pb = id(b)

#Result
#There is no pointer concept in python and can't access memory location directly by user
sys.stdout.write("\nValue of a = %d & b = %d"%(a,b))
sys.stdout.write("\nAddress of a = %u"%(pa))
sys.stdout.write("\nAddress of b = %u"%(pb))
sys.stdout.write("\n\nAddition of 'a' & 'b' : %d"%(a+b))

a = b
pa = id(a)
#In python, variables of same value are tagged together instead of storing the value in different locations

sys.stdout.write("\n*pa = %d *pb = %d"%(a,b))
sys.stdout.write("\npa = %u pb = %u"%(pa,pb))
sys.stdout.write("\nAddition of *pa + *pb : %d"%(a+b))
Enter Value of 'a' & 'b' : 8
Enter Value of 'a' & 'b' : 4

Value of a = 8 & b = 4
Address of a = 19555076
Address of b = 19555124

Addition of 'a' & 'b' : 12
*pa = 4 *pb = 4
pa = 19555124 pb = 19555124
Addition of *pa + *pb : 8

Example 9.11, Page number: 290

In [8]:
#The effect of increment on pointer variables. 

import sys

#Variable Initialization
x = int(raw_input("Enter integer Value"))
y = raw_input("Enter Character Value")
z = float(raw_input("Enter float Value"))

x1 = id(x)
y1 = id(y)
z1 = id(z)

#Result
#There is no pointer concept in python and can't access memory location directly by user

sys.stdout.write("\nAddress of x = %u\n"%(x1))
sys.stdout.write("\nAddress of y = %u\n"%(y1))
sys.stdout.write("\nAddress of z = %u\n"%(z1))

x1 += 1
y1 += 1
z1 += 1
#In python, variables of same value are tagged together instead of storing the value in different locations

sys.stdout.write("\nAfter Increment in Pointers\n")
sys.stdout.write("\nNow Address of x = %u\n"%(x1))
sys.stdout.write("Now Address of y = %u\n"%(y1))
sys.stdout.write("Now Address of z = %u\n"%(z1))

sys.stdout.write("\nSize of Integer : %d"%(sys.getsizeof(x)))
sys.stdout.write("\nSize of Character :  %d"%(sys.getsizeof(y)))
sys.stdout.write("\nSize of Float : %d"%(sys.getsizeof(z)))
Enter integer Value2
Enter Character ValueA
Enter float Value2.2

Address of x = 19555148

Address of y = 48769192

Address of z = 44777728

After Increment in Pointers

Now Address of x = 19555149
Now Address of y = 48769193
Now Address of z = 44777729

Size of Integer : 12
Size of Character :  22
Size of Float : 16

Example 9.12, Page number: 291

In [9]:
#The effect of increased and decreased operator used as prefix and suffix with pointer variable.

import sys

#Variable Initialization
i = int(raw_input("Enter Value of i = "))
ii = id(i)

#Result
sys.stdout.write("Address of i = %u\n"%(ii))
#There is no increment or decrement operator and pointer concept in python
ii += 1
sys.stdout.write("Address of i = %u\n"%(ii))
sys.stdout.write("Address of i = %u\n"%(ii))
ii += 1
ii -= 1
sys.stdout.write("Address of i = %u\n"%(ii))
sys.stdout.write("Address of i = %u\n"%(ii))
ii -= 1
sys.stdout.write("Address of i = %u\n"%(ii))

#Memory address differs in different systems
Enter Value of i = 8
Address of i = 19555076
Address of i = 19555077
Address of i = 19555077
Address of i = 19555077
Address of i = 19555077
Address of i = 19555076

Example 9.13, Page number: 292

In [3]:
#Perform different arithmetic operations using pointers

import sys

#Variable Initialization
a = 25
b = 10
p = id(a)
j = id(b)

#Result
#There is no pointer concept in python and user can't access memory contents directly
sys.stdout.write("\nAddition a + b = %d"%(a+b))
sys.stdout.write("\nSubtraction a - b = %d"%(a-b))
sys.stdout.write("\nProduct a * b = %d"%(a*b))
sys.stdout.write("\nDivision a / b = %d"%(a/b))
sys.stdout.write("\na Mod b  = %d"%(a%b))
Addition a + b = 35
Subtraction a - b = 15
Product a * b = 250
Division a / b = 2
a Mod b  = 5

Example 9.14, Page number: 293

In [5]:
#Compare two pointers. 

import sys

#Variable Initialization
a = 2
j = id(a)
k = id(a)

#Result
if j == k:
    sys.stdout.write("\nThe Tow Pointers have the same address.")
else:
    sys.stdout.write("\nThe Pointers have the different address.")
    
sys.stdout.write("\n\nAddress of a (j) = %u"%(j))
sys.stdout.write("\nAddress of a (k) = %u"%(k))
#Memory address differs in different systems
The Tow Pointers have the same address.

Address of a (j) = 30726364
Address of a (k) = 30726364

Example 9.15, Page number: 294

In [7]:
#Display elements of an array.  

import sys

#Variable Initialization
x = [2,4,6,8,10]
k = 1

#Result
while k <= 5:
    sys.stdout.write("%3d"%(x[k-1]))
    k += 1
    
#There is no pointer concept in python
  2  4  6  8 10

Example 9.16, Page number: 295

In [11]:
#Display array element with their addresses using array name as a pointer

import sys

#Variable Initialization
x = [2,4,6,8,10]
k = 0

#Result
sys.stdout.write("\nElement No.\tElement\tAddress")

while k < 5:
    sys.stdout.write("\nx[%d] \t%13d %10u"%(k,x[k],id(x[k])))
    k += 1
    
#There is no pointer concept in python
Element No.	Element	Address
x[0] 	            2   30726364
x[1] 	            4   30726340
x[2] 	            6   30726316
x[3] 	            8   30726292
x[4] 	           10   30726268

Example 9.17, Page number: 296

In [17]:
#Display array elements with their addresses using array name as a pointer

import sys

#Variable Initialization
num = [10,25,35,45]

#Result
sys.stdout.write("\nElement\t           Address\n")

for i in range(0,4):
    sys.stdout.write("num[%d] = %d        "%(i,num[i]))
    sys.stdout.write("%8u\n"%(id(num[i])))
    
#There is no pointer concept in python
Element	           Address
num[0] = 10        30726268
num[1] = 25        30726088
num[2] = 35        30725968
num[3] = 45        30725848

Example 9.18, Page number: 296

In [18]:
#Access elements of array through different ways using pointer

import sys

#Variable Initialization
arr = [10,20,30,40,50]
p = 0

#Result
for p in range(0,5):
    sys.stdout.write("Value of arr[%d] = "%(p))
    sys.stdout.write("%d   |  "%(arr[p]))
    sys.stdout.write("%d   |  "%(arr[p]))
    sys.stdout.write("%d   |  "%(arr[p]))
    sys.stdout.write("%d   |  "%(arr[p]))
    sys.stdout.write("address of arr[%d] = %u\n"%(p,id(arr[p])))
    
#There is no pointer concept in python
#Memory address differs in different systems
Value of arr[0] = 10   |  10   |  10   |  10   |  address of arr[0] = 30726268
Value of arr[1] = 20   |  20   |  20   |  20   |  address of arr[1] = 30726148
Value of arr[2] = 30   |  30   |  30   |  30   |  address of arr[2] = 30726028
Value of arr[3] = 40   |  40   |  40   |  40   |  address of arr[3] = 30725908
Value of arr[4] = 50   |  50   |  50   |  50   |  address of arr[4] = 30725788

Example 9.19, Page number: 297

In [20]:
#Find sum of all the elements of an array.

import sys

#Variable Initialization
sum1 = 0        #Since sum is a built-in function in python, sum1 is used
i = 0
a = [1,2,3,4,5]

#Result
sys.stdout.write("Elements       Values         Address\n\n")

while i < 5:
    sys.stdout.write("a[%d]\t   %5d\t    %8u\n"%(i,a[i],id(a[i])))
    sum1 = sum1 + a[i]
    i += 1
    
#There is no pointer concept in python
Elements       Values         Address

a[0]	       1	    30726376
a[1]	       2	    30726364
a[2]	       3	    30726352
a[3]	       4	    30726340
a[4]	       5	    30726328

Example 9.20, Page number: 298

In [21]:
#Sum of squares and cubes of array elements using pointer

import sys

#Variable Initialization
b = [1,2,3,4,5]
sumsq = 0
sumc = 0

#Result
for j in range(0,5):
    sumsq = sumsq + pow(b[j],2)
    sumc = sumc + pow(b[j],3)
    
sys.stdout.write("\nSum of Squares of array elements : %d"%(sumsq))
sys.stdout.write("\nSum of Cubes of array elements : %d"%(sumc))

#There is no pointer concept in python
Sum of Squares of array elements : 55
Sum of Cubes of array elements : 225

Example 9.21, Page number: 299

In [23]:
#Copy element of one array to another array using pointers.

import sys

#Variable Initialization
so = [10,20,30,40,50]
ds = [0 for i in range(0,5)]

#Copying
for i in range(0,5):
    ds[i] = so[i]
    
sys.stdout.write("Original Array      Duplicated Array")

for i in range(0,5):
    sys.stdout.write("\n\t%d\t\t%d"%(so[i],ds[i]))
    
#There is no pointer concept in python
Original Array      Duplicated Array
	10		10
	20		20
	30		30
	40		40
	50		50

Example 9.22, Page number: 299

In [24]:
#Copy one array into another array. 

import sys

#Variable Initialization
arr1 = [15,25,35,45,55]
arr2 = [0 for i in range(0,5)]
j = 4

#Copying & Result
sys.stdout.write("\nOriginal Array     Duplicate Array")
for i in range(0,5):
    arr2[i] = arr1[j]
    j -= 1
    sys.stdout.write("\n\t%d\t\t%d"%(arr1[i],arr2[i]))
    
#There is no pointer concept in python
Original Array     Duplicate Array
	15		55
	25		45
	35		35
	45		25
	55		15

Example 9.23, Page number: 300

In [25]:
#Display array elements and their address using pointers

import sys

#Variable Initialization
a = [[1,2,3],[4,5,6],[7,8,9]]
j = 1

#Result
sys.stdout.write("\tElements of An Array with their addresses\n\n")

p = id(a[0][0])

for i in range(0,3):
    for j in range(0,3):
        sys.stdout.write("%5d  [%5u]"%(a[i][j],id(a[i][j])))
    sys.stdout.write("\n")
    
#There is no pointer concept in python
#Memory address differs in different systems
	Elements of An Array with their addresses

    1  [30726376]    2  [30726364]    3  [30726352]
    4  [30726340]    5  [30726328]    6  [30726316]
    7  [30726304]    8  [30726292]    9  [30726280]

Example 9.24, Page number: 301

In [26]:
#Display array elements and their address. 

import sys

#Variable Initialization
a = [[1,2,3],[4,5,6],[7,8,9]]

#Result
sys.stdout.write("\nElements of An Array with their addresses.\n\n")

for i in range(0,3):
    for j in range(0,3):
        sys.stdout.write("%5d  [%5u]"%(a[i][j],id(a[i][j])))
    sys.stdout.write("\n")
    
#There is no pointer concept in python
#Memory address differs in different systems
Elements of An Array with their addresses.

    1  [30726376]    2  [30726364]    3  [30726352]
    4  [30726340]    5  [30726328]    6  [30726316]
    7  [30726304]    8  [30726292]    9  [30726280]

Example 9.25, Page number: 302

In [29]:
#Store addresses of different elements of an array using array of pointers

import sys

#Variable Initialization
arrl = [5,10,15]
arrp = [0 for i in range(0,3)]

for i in range(0,3):
    arrp[i] = id(arrl[i])
    
#Result
sys.stdout.write("\nAddress  \tElement\n")

for k in range(0,3):
    sys.stdout.write("%u"%(arrp[k]))
    sys.stdout.write("\t%7d\n"%(arrl[k]))
    
#There is no pointer concept in python
#Memory address differs in different systems
Address  	Element
30726328	      5
30726268	     10
30726208	     15

Example 9.26, Page number: 303

In [31]:
#Display address of elements and pointers

import sys

#Variable Initialization
a = [0,1,2,3,4]
p = [0 for i in range(0,5)]

for i in range(0,5):
    p[i] = id(a[i])
    
#Result
for i in range(0,5):
    sys.stdout.write("\n%d at location"%(a[i]))
    sys.stdout.write("\t%u at location"%(id(a[i])))
    sys.stdout.write("   %u"%(id(p[i])))
    
#There is no pointer concept in python
#Memory address differs in different systems
0 at location	30726388 at location   52456564
1 at location	30726376 at location   52456360
2 at location	30726364 at location   52456456
3 at location	30726352 at location   52456420
4 at location	30726340 at location   52456468

Example 9.27, Page number: 304

In [33]:
#Print value of a variable through pointer and pointer to pointer

import sys

#Variable Initialization
a = 2
p = id(a)
q = id(p)

#Result
sys.stdout.write("\n        Value of a = %d Address of a = %u"%(a,id(a)))
sys.stdout.write("\nThrough *p Value of a = %d Address of a = %u"%(a,p))
sys.stdout.write("\nThrough **q Vallue of a = %d Address of a = %d"%(a,p))

#There is no pointer concept in python
#Memory address differs in different systems
        Value of a = 2 Address of a = 30726364
Through *p Value of a = 2 Address of a = 30726364
Through **q Vallue of a = 2 Address of a = 30726364

Example 9.28, Page number: 305

In [36]:
#Different levels of array of pointer to pointer and display the elements

import sys

#Variable Initialization
a = [1,2,3]
b = [0 for i in range(0,3)]
c = [0 for i in range(0,3)]
d = [0 for i in range(0,3)]
e = [0 for i in range(0,3)]
f = [0 for i in range(0,3)]

for k in range(0,3):
    b[k] = id(a[k])
    c[k] = id(b[k])
    d[k] = id(c[k])
    e[k] = id(d[k])
    f[k] = id(e[k])
    
#Result
for k in range(0,3):
    sys.stdout.write("%3d"%(a[k]))
    sys.stdout.write("%3d"%(a[k]))
    sys.stdout.write("%3d"%(a[k]))
    sys.stdout.write("%3d"%(a[k]))
    sys.stdout.write("%3d\n"%(a[k]))
    
#There is no pointer concept in python
#Memory address differs in different systems
  1  1  1  1  1
  2  2  2  2  2
  3  3  3  3  3

Example 9.29, Page number: 306

In [37]:
#Display string using character pointer

import sys

#Variable Initialization
name = ['K','U','M','A','R']
ch = id(name)
i = 0

#Result
while i < len(name):                 #There is no null terminating character in python string
    sys.stdout.write("%c"%(name[i]))
    i += 1
    
#There is no pointer concept in python
KUMAR

Example 9.30, Page number: 307

In [40]:
#Length of a given string including and excluding spaces using pointers

import sys

#Variable Initialization
p = 0
q = 0
s = 0
str1 = ['P','O','I','N','T','E','R','S',' ','A','R','E',' ','E','A','S','Y']

#Result
while s < len(str1):          #There is no null termination character for python string
    sys.stdout.write("%c"%(str1[s]))
    p += 1
    if ord(str1[s]) == 32:
        q += 1
    s += 1

sys.stdout.write("\nLength of String including spaces : %d"%(p))
sys.stdout.write("\nLength of String excluding spaces : %d"%(p-q))

#There is no pointer concept in python
POINTERS ARE EASY
Length of String including spaces : 17
Length of String excluding spaces : 15

Example 9.31, Page number: 308

In [41]:
#Interchange elements of character array using pointer

import sys

#Variable Initialization
names = ["kapil","manoj","amit","amol","pavan","mahesh"]

#Result
sys.stdout.write("Original : %s  %s"%(names[3],names[4]))

tmp = names[3]
names[3] = names[4]
names[4] = tmp

sys.stdout.write("\nNew    : %s  %s"%(names[3],names[4]))

#There is no pointer concept in python
Original : amol  pavan
New    : pavan  amol

Example 9.32, Page number: 308

In [1]:
#String comparison.

import sys

#Variable Initialization
c = 0
str1 = raw_input("Enter First String : ")
str2 = raw_input("Enter Second String : ")
a = 0
l = 0

#Result
sys.stdout.write("\nSimilar Characters Found in Both String")

while a < len(str1):
    if str1[a] == str2[a]:
        sys.stdout.write("\n\t%c\t%c"%(str1[a],str2[a]))
        l += 1
    else:
        c += 1
    a += 1
        
if c == 0:
    sys.stdout.write("\nThe Strings are Identical")
else:
    sys.stdout.write("\nThe Strings are different at %d places."%(c))
    sys.stdout.write("\nThe String Characters are similar at %d places."%(l))
    
#There is no pointer concept in python
Enter First String : SUNDAY
Enter Second String : MONDAY

Similar Characters Found in Both String
	N	N
	D	D
	A	A
	Y	Y
The Strings are different at 2 places.
The String Characters are similar at 4 places.

Example 9.33, Page number: 310

In [2]:
#Compare three characters using pointers.

import sys

#Variable Initialization
x = raw_input("Enter Three Characters")
y = raw_input("Enter Three Characters")
z = raw_input("Enter Three Characters")

stat = 0

stat = y > x
if x == y:
    sys.stdout.write("\n1st and 2nd Character are same\n")
else:
    if stat < 0:
        sys.stdout.write("\n2nd Character appears after the 1st Character in Alphabetic\n")
    else:
        sys.stdout.write("2nd Character appears before the first Character in Alphabetic\n")
    
stat = y > z

if y == z:
    sys.stdout.write("\n2nd and 3rd Character are same.")
else:
    if stat > 0:
        sys.stdout.write("\n2nd Character appears after the 3rd Character in Alphabetic")
    else:
        sys.stdout.write("\n2nd Character appears before the 3rd Character in Alphabetic")
Enter Three CharactersC
Enter Three CharactersC
Enter Three CharactersA

1st and 2nd Character are same

2nd Character appears after the 3rd Character in Alphabetic

Example 9.34, Page number: 311

In [6]:
#Compare two strings irrespective of case.  

import sys

#Variable Initialization
buf1 = "computer"
buf2 = "computer"

#Comparison
stat = buf1 == buf2

#Result
sys.stdout.write("The Characters upto 4th position are ")
if stat == 0:
    sys.stdout.write("not ")
sys.stdout.write("same")

#There is no pointer concept in python and memicmp() function
The Characters upto 4th position are same

Example 9.35, Page number: 311

In [3]:
#Print the string upto the first occurrence of a character

import sys

#Variable Initialization

src = raw_input("Enter a String : ")
f = raw_input("Enter a Character to find in the text : ")
dest = ['0' for i in range(0,len(src))]
ptr = 0

while ptr < len(src):
    if src[ptr] == f:
        dest[ptr] = src[ptr]
        break
    else:
        dest[ptr] = src[ptr]
    ptr += 1

if ptr == len(src):
    sys.stdout.write("The character wasn't found\n")
else:
    sys.stdout.write("String upto that Character : %s"%(dest[0:ptr+1]))
    
Enter a String : FUNCTIONS
Enter a Character to find in the text : T
String upto that Character : ['F', 'U', 'N', 'C', 'T']

Example 9.36, Page number: 312

In [5]:
#Replace the contents of second string with the first string. 

import sys

#Variable Initialization
src = raw_input("Enter a Source String : ")
dest = raw_input("Enter a Destination String :")

#Result
sys.stdout.write("\n\nDestination before memcpy : %s\n"%(dest))
dest = src[:len(src)] + dest[len(src):]

sys.stdout.write("Destination after memcpy : %s\n"%(dest))

#There is no pointer concept in python and memcpy function
Enter a Source String : Tomorrow
Enter a Destination String :Today   is Sunday


Destination before memcpy : Today   is Sunday
Destination after memcpy : Tomorrowis Sunday

Example 9.37, Page number: 313

In [16]:
#Display the string through their pointer

import sys

#Variable Initialization
c = "Central Processing Unit"
m = "Math Co- Processor"

#Result
sys.stdout.write("'c' is pointing the string '%s'\n"%(c))
sys.stdout.write("'m' is pointing the string '%s'\n"%(m))

#There is no pointer concept in python
'c' is pointing the string 'Central Processing Unit'
'm' is pointing the string 'Math Co- Processor'

Example 9.38, Page number: 314

In [18]:
#Use void pointer to display the value of different variables.

import sys

#Variable Initialization & Result
pt = 12
sys.stdout.write("\nP = %d"%(pt))
pt = 5.4
sys.stdout.write("\nR = %.1f"%(pt))
pt = 'S'
sys.stdout.write("\nC = %c"%(pt))

#There is no pointer concept in python
P = 12
R = 5.4
C = S
In [ ]: