Anda di halaman 1dari 18

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name : Murali Krishna Chintala
Designation : Lecturer in CME
Branch : Computer Engineering
Institute : SUVR & SR GPW, Ethamukkala
Year/Semester : III Semester
Subject : UNIX & C
Subject Code : CM – 304
Topic : Basics of Pointers
Duration : 50 Min
Sub Topic : Pointer arrays
Teaching Aids : PPT, Animations
CM304.73 1
Objective

On completion of this period, you would be able to


Know …

 Understand the concept of pointer arrays with


examples

CM304.73 2
Pointers and Arrays
 An array of pointers is an array that contains
pointers as it’s elements.

 Pointer arrays give a convenient method for


storing strings.

 As strings are character arrays, each array


element is a character-type pointer that points
to a separate string.
CM304.73 3
Pointers and Arrays
Contd..

 Suppose the following strings are to be stored in


a character-type array.

 Bombay
 Delhi
 Madras
 Kolkata
 Hyderabad

CM304.73 4
Pointers and Arrays
Contd..
 The above strings can be stored in a two
dimensional character-type array declared as
char city[5][12];

 Note that city contains 5 rows to accommodate


the five strings.

 Each row must be large enough to store at


least 12 characters as well as the null
character(‘\0’) at the end.
CM304.73 5
Pointers and Arrays
Contd..

A better way to do this is to define a 5 element


character array of pointers as

char *city[5]; so that

 city[0] points to Bombay

 city[1] points to Delhi and so on.


CM304.73 6
Pointers and Arrays
Contd..

 It is not necessary to include the maximum string


size with in the array declaration.

 A specified amount of memory will be allocated


for each string later in the program using

city[i]=(char *)malloc(12 * size of(char));

CM304.73 7
Pointers and Arrays
Contd..

Reasons for using array of pointers to store


strings are

 Efficient use of available memory.

 Manipulation of strings is easier.

 Long strings can be stored with minimum effort.

CM304.73 8
Pointers and Arrays
Contd..

/*Program to interchange elements of character


array using pointers*/
#include<stdio.h>
main()
{
char *names[]={“Hemanth”,”sirisha”,”sridevi”};
char *temp;

CM304.73 9
Contd..

Pointers and arrays

printf(“original %s%s”,names[1],names[2]);

temp=names[1];

names[1]=names[2];

names[2]=temp;

printf(“new %s%s”,names[1],names[2]);

}
CM304.73 10
Pointers and Arrays
Contd..

Output:

 Original : Sirisha Sridevi

 New : Sridevi Sirisha

CM304.73 11
Summary

In this class, we have leant about…


 Pointer arrays give a convenient method for
storing strings.
 Reasons for using arrays of pointers to store
strings are
 Efficient use of available memory.
 Manipulation of strings is easier.
 Long strings can be stored with minimum
effort.
CM304.73 12
Quiz

1.Pointer arrays give a convenient method for


storing strings

a) True

b) False

c) None

CM304.73 13
Quiz

1.Pointer arrays give a convenient method for


storing strings

a) True

b) False

c) None

CM304.73 14
Quiz

2.Reasons for using array of pointers to store


strings are

a) Efficient use of available memory

b) Manipulation of strings is easier

c) both

CM304.73 15
Quiz

2.Reasons for using array of pointers to store


strings are

a) Efficient use of available memory

b) Manipulation of strings is easier

c) both

CM304.73 16
Assignment

1) Write a program to find the biggest number in


an array using pointers?

2) Write a program to reverse an array using


pointers?

CM304.73 17
Frequently asked questions

1) Write a ‘C’ program to find the length of a


given string using pointers?
(Apr’04)

CM304.73 18