# Chapter 5: Arrays and Pointers¶

## Example 5.1, page no. 137¶

In :
'''
python does not have pointer functionality.We can get it explicitly.
'''

class ref:
def __init__(self, obj): self.obj = obj
def get(self):    return str(self.obj)
def set(self, obj):      self.obj = obj

x=ref()
p=x # assigning variable value to p

p.set(0) # set x to zero */
print "\nx is " + x.get()
print "\np is " + p.get()
a = p.get()
p.set(int(a)+1) # increment what p points to */
print "\nx is " +x.get()
a = p.get()
p.set(int(a)+1) # increment what p points to */
print "\nx is " + x.get()

x is 0

p is 0

x is 1

x is 2


## Example 5.2, page no. 139¶

In :
def date(a,b): # /* declare the function */
day_ret =0
month_ret = 0
''' At this point, calculate the day and month
* values in day_ret and month_ret respectively.
'''
a = day_ret;
b = month_ret;
return a,b

month = 5
day = 1
d,m = date(day,month);
print "day is %d, month is %d\n" %(d, m)

day is 0, month is 0



## Example 5.3, page no. 142¶

In :
from array import array

ARSZ=20
ar=[]

for i in range(ARSZ + 1):
ar.append( i)
print "ar[%d] now = %d\n" %( i, ar[i])

ar now = 0

ar now = 1

ar now = 2

ar now = 3

ar now = 4

ar now = 5

ar now = 6

ar now = 7

ar now = 8

ar now = 9

ar now = 10

ar now = 11

ar now = 12

ar now = 13

ar now = 14

ar now = 15

ar now = 16

ar now = 17

ar now = 18

ar now = 19

ar now = 20



## Example 5.4 & 5.5, page no. 146¶

In :
ARSZ = 10

fp1= ARSZ
fp2=0

while(fp2 != fp1):
print "Difference: %d\n" %((ARSZ-fp1))
fp1 -= 1

Difference: 0

Difference: 1

Difference: 2

Difference: 3

Difference: 4

Difference: 5

Difference: 6

Difference: 7

Difference: 8

Difference: 9



## Example 5.6, page no. 149¶

In :
s = raw_input("Enter String:")
print len(s)

Enter String:python
7


## Example 5.7, page no. 150¶

In :
def str_eq(s1,s2):
if s1==s2:
return True
return False

print str_eq("this","hello")
print str_eq("this","this")

False
True


## Example 5.8, page no. 151¶

In :
cp = "a string";
for i in cp:
print i,

print "\n"
for i in range(len(cp)):
print cp[i],

a   s t r i n g

a   s t r i n g


## Example 5.9, page no. 152¶

In :
import numpy

ARLEN=10
ar  =  numpy.zeros(ARLEN)

print ar

[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]


## Example 5.10, page no. 158¶

In :
'''
Python does not have any concept of pointer.
Python will go same for all mentioned.
'''

MAXSTRING=50 # /* max no. of strings */
MAXLEN = 80 #/* max length. of strings */

strings = [] # list of strings

def print_arr(a):
print a

def sort_arr(a):
a.sort()
return a

def next_string():
s = raw_input("Enter Next String : ")
strings.append(s)
return s

nstrings = 0

while(nstrings < MAXSTRING and next_string() != '0'):
nstrings += 1

print_arr(strings)
print sort_arr(strings)

Enter Next String : abc
Enter Next String : def
Enter Next String : fed
Enter Next String : 0
['abc', 'def', 'fed', '0']
['0', 'abc', 'def', 'fed']


## Example 5.13, page no. 165¶

In :
'''
We need not to worry about memory allocation and deallocation in python.
It is simple to get a string, print and copy it.
'''

str_p = ""
tmp_p = ""

str_p = raw_input("Enter String.. : ")
next_p = str_p

tmp_p = str_p

print tmp_p

print str_p

Enter String.. : hello
hello
hello


## Example 5.14, page no. 165¶

In :
arr = "hello"
a = arr
print "Size of arr %d\n" % len(arr)
print "Size of a %d\n" % len(a)

Size of arr 5

Size of a 5



## Example 5.16, page no. 167¶

In :
def func(arg):
print "%d\n" %(arg)

func(5)

5