You are on page 1of 18

# DEPARTMENT OF TECHNICAL EDUCATION

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

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.

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
 Kolkata

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

##  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..

strings are

##  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:

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

storing strings

a) True

b) False

c) None

CM304.73 13
Quiz

storing strings

a) True

b) False

c) None

CM304.73 14
Quiz

strings are

c) both

CM304.73 15
Quiz

strings are

c) both

CM304.73 16
Assignment

## 1) Write a program to find the biggest number in

an array using pointers?

pointers?

CM304.73 17