Chapter 18 : Pointers and Structures

Example: 1, Page No.: 5.130

In [2]:
#Program to print details of person using structure and pointer

class account:
    name=""
    place=""
    acctno=0
    
m4=account()
m4.name="Venkat"
m4.place="Arcot"
m4.acctno=24201

print "%s\t%s\t%d\n"%(m4.name,m4.place,m4.acctno)

contents=m4

print "%s\t%s\t%d\n"%(contents.name,contents.place,contents.acctno)
Venkat	Arcot	24201

Venkat	Arcot	24201

Example: 2, Page No.: 5.131

In [7]:
#Program to print name, place, and account number using structure and pointer

class account:
    name=""
    place=""
    acctno=0
    
m4=account()
m4.name="Madhavan"
m4.place="Chennai"
m4.acctno=241
a=m4

print "%s\n%s\n%d\n"%(a.name,a.place,a.acctno)
Madhavan
Chennai
241

Example: 3, Page No.: 5.132

In [15]:
#Program to show how the int_pointers structure can be handled.


class int_pointers:
    p1=0
    p2=0
    
pointers=int_pointers()
m1= 80
pointers.p1=m1
pointers.p2=-40
m2=pointers.p2
print "m1= %i, *pointers.p1 = %i\n"% (m1,pointers.p1)
print "m2= %i, *pointers.p2 = %i\n"% (m2,pointers.p2)
m1= 80, *pointers.p1 = 80

m2= -40, *pointers.p2 = -40

Case Study 1: Page No.5.133

In [16]:
#Program to interchange the values stored in two variables using function with pointers. 

def interchange(a,b):
    t=a
    a=b
    b=t
    return b,a
    
a=39
b=77

print "Before Interchange: a=%d, b=%d"%(a,b)


print "After Interchange : a=%d, b=%d"%(interchange(b,a))
Before Interchange: a=39, b=77
After Interchange : a=77, b=39

Case Study: 2, Page No.:5.134

In [46]:
#Program to demonstrate the use of structure pointers

class book:
    bno=0
    bname=''
    cost=0.0

p=[book()for i in range(0,3)]
print "Enter the Book details... "
print "Enter Book number, Book Name, Book Costs... "
for i in range(0,3):
    p[i].bno=input()
    p[i].bname=raw_input()
    p[i].cost=input()
    
for i in range(0,3):
   
    print "\n%d\t%s\t\t%d"%(p[i].bno,p[i].bname,p[i].cost)
   
print "\nStructure Output\n"
ptr=p

for i in range(0,3):
     print "%d\t%s\t\t%d"%(ptr[i].bno,ptr[i].bname,ptr[i].cost)
    
Enter the Book details... 
Enter Book number, Book Name, Book Costs... 
101
Computer Practice_1
240
201
Computer Practice_2
250
202
C++ and JAVA
280

101	Computer Practice_1	240

201	Computer Practice_2	250

202	C++ and JAVA	280

Structure Output

101	Computer Practice_1	240
201	Computer Practice_2	250
202	C++ and JAVA	280

Case Study 3: Page No: 5.136

In [3]:
#Program to count number of vowels,consonants, digits,spaces and other characters in a line of text.

vowels='aeiou'
consonants='bcdfghjklmnpqrstvwxyz'
spaces =' '
digits='1234567890'
special_characters='!@#$%^&*()_'

text=raw_input("Enter the text in lower case: ")

print "\nNumber of vowels: ",dict((v, text.count(v)) for v in text if v in vowels)
print "Number of consonants: ",dict((c, text.count(c)) for c in text if c in consonants)
print "Number of Spaces: ",dict((s, text.count(s)) for s in text if s in spaces)
print "Number of digits: ",dict((d, text.count(d)) for d in text if d in digits)
print "Number of special_characters: ",dict((sc, text.count(sc)) for sc in text if sc in special_characters)
Enter the text in lower case: 123 vrb publishers @#$%^&*()

Number of vowels:  {'i': 1, 'e': 1, 'u': 1}
Number of consonants:  {'b': 2, 'h': 1, 'l': 1, 'p': 1, 's': 2, 'r': 2, 'v': 1}
Number of Spaces:  {' ': 3}
Number of digits:  {'1': 1, '3': 1, '2': 1}
Number of special_characters:  {'@': 1, '#': 1, '%': 1, '$': 1, '&': 1, ')': 1, '(': 1, '*': 1, '^': 1}

Case Study 4: Page No.: 5.136

In [22]:
#Program to demonstrate malloc() function

NULL=0
string = ''
if string==NULL:
    print "Not Enough memory to allocate buffer\n"
else:
    
    def strcpy(cstring1):
          import copy
          cstring2=copy.copy(cstring1)
          return cstring2  
string="Hello"
print "String is %s" %string
String is Hello

Case Study 5: Page No.: 5.137

In [20]:
#Program to demonstrate calloc() function

string=''

def strcpy(cstring1):
          import copy
          cstring2=copy.copy(cstring1)
          return cstring2  
stri="BHAVANA"
string=strcpy(stri)
print "String is %s" %string
String is BHAVANA

Case Study 6: Page No.:5.138

In [29]:
#Program to demonstrate realloc() function
#Cannot do in python