Skip to main content
Unit III
Arrays and Pointers
L.NIVETHA AP/CSE,KNCET
Introduction to Arrays – Single
Dimensional Arrays – Multidimensional
Array – Pointers – void Pointer – Null
Pointer – Relationship between Arrays
and Pointers – Arrays of Pointers –
Pointer to a Pointer – Pointer to an Array.
L.NIVETHA AP/CSE,KNCET
ARRAYS & its types
Presented by,
L.Nivetha,AP/CSE,
KNCET
KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY
(AUTONOMOUS)
NAMAKKAL - TRICHY MAIN ROAD, THOTTIAM, TRICHY-621 215
L.NIVETHA AP/CSE,KNCET
ARRAY - Definition
 An array is a data structure, that is
used to store the homogeneous data.
 An array is a collection of similar data
items that are stored under a common
name.
 A value in an array is identified by it’s
index or subscript.
 subscript enclosed in square brackets
with an array name.
 Array is derived data type
L.NIVETHA AP/CSE,KNCET
Types of Array
Array can be classified into:
• One dimensional Array
• Two dimensional Array
• Multi dimensional Array
L.NIVETHA AP/CSE,KNCET
1. One Dimensional Array
 The collection of data items can be stored
under a single variable name using only one
subscript
 so such a variable called one dimensional
Array.
 It store fixed number of elements with same
data type.
 It is organized in a linear sequence.
 It is also named as linear array and vector.
L.NIVETHA AP/CSE,KNCET
Array Declaration:
Array are declared in the same manner as an
ordinary variables except that each array name must
have one subscript.
Syntax:
data type array_variable[size or subscript of the
array];
Example:
• int a[5];
a[0]
a[1]
a[2]
a[3]
a[4]
L.NIVETHA AP/CSE,KNCET
Array Initialization
The value can be initialized to an array,
when they are declared like ordinary
variables. Otherwise they hold garbage
value.
The array can be initialized into two way’s:
1. At compile time
2. At run time
L.NIVETHA AP/CSE,KNCET
At compile time:
syntax: data type array_name[size] =
{list of values}
Example: int marks[3] = {98,67,97};
(or)
marks[0]=98;
marks[1]=67;
marks[2]=97;
L.NIVETHA AP/CSE,KNCET
Example Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int b[3] = {2,4,5} , i ;
clrscr();
for(i=0; i<3 ;i++)
{
Printf(“%d”, b[i]);
}
}
L.NIVETHA AP/CSE,KNCET
At run time initialization:
The array can be explicitly initialized at run
time.
Here we use the for loop as well as scanf()
statement to get a value of an array variable.
Example:1
int n[3],i;
for(i=0 ; i<3 ; i++)
{
scanf( “%d”, &n[i] );
}
L.NIVETHA AP/CSE,KNCET
Example program:
#include<stdio.h>
void main()
{
int mark[3], i;
printf(“enter the marks:”);
for(i=0 ; i<3 ; i++)
{
scanf( “%d”, &mark[i] );
}
for(i=0 ; i<3 ; i++)
{
printf( “%d”, mark[i] );
}
}
L.NIVETHA AP/CSE,KNCET
Assigning an array to another array:
To assign the one array element into another
array by using it’s index position.
 Here not possible to assign array1=array2.
 variable only possible to assign like this
assignment.
Example :
int a[3], b[3]={2,4,6}, i;
for(i=0;i<3;i++)
a[i]=b[i]
L.NIVETHA AP/CSE,KNCET
Two Dimensional Array
The array variable declared using two
subscript then it is called as two dimensional
array.
Two dimensional array are used to store table
of values also called as matrix.
It’s element arranged in rectangular grid of
rows and column.
to access this data by using both column and
row subscript.
L.NIVETHA AP/CSE,KNCET
Declaring two dimensional array
To declare the two dimensional array with two
subscript.
First subscript with row size and 2nd subscript
with column size.
data type and identifier with row and column
subscript are mandatory.
L.NIVETHA AP/CSE,KNCET
Syntax:
data_type array_name[row size][column size];
Example:
int a[3][3];
col0 col1 col2
Row0
Row1
Row2
A[0][0] A[0][1] A[0][2]
A[1][0] A[1][1] A[1][2]
A[2][0] A[2][1] A[2][2]
L.NIVETHA AP/CSE,KNCET
Initializing a two dimensional array:
To initialize two dimensional array like one
dimensional array
The number of values initialize in an array is
less than or equal to array size(row size * column
size)
Example:
int a[2][2]={
{23,34},
{43,45},
};
Or
int a[2][2]={89,10,34,34};
L.NIVETHA AP/CSE,KNCET
Example Program:
#include<stdio.h>
#include<conio.h>
void main()
{ 0 1
int s[3][2]= {0{ 23,34 },
1{ 45,45 },
2{ 76,86 } }
int i , j ;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(“%d”, s[i][j]);
}
printf(“n”);
}
}
L.NIVETHA AP/CSE,KNCET
Multidimensional Array
If a variable having more than two
subscript then those array variable is called as
multidimensional array
three dimensional arrays can be visualized
as a cube that has a number of planes
Each plane is a two dimensional arrays, so
we made a three dimensional array using
many two dimensional array.
L.NIVETHA AP/CSE,KNCET
Declaring three dimensional array:
plane, row and column specifier are compile
time constant, it must be greater than zero.
Syntax:
data_type
array_variable[plane_specifier][row_specifier]
[column_specifier];
Example
Int a[2][5][3];
L.NIVETHA AP/CSE,KNCET
Int a;
a= 15;
Int *b;
b=&a;
L.NIVETHA AP/CSE,KNCET
POINTER
Pointer is a variable that contains
memory address of a normal variable or a
function.
it is a derived data type in C language.
pointer can be used to access and
manipulate data stored in the memory.
L.NIVETHA AP/CSE,KNCET
Pointer allow dynamic memory management
Pointers provide an efficient tool for
manipulating dynamic data structure
such as structures , linked list , queues ,
stacks and trees.
Pointer reduce length and complexity of the
program.
Advantages of pointer
L.NIVETHA AP/CSE,KNCET
 pointer variable declaration is same as normal variable
declaration ,
 In additionally we add ‘*’symbol before the variable
name then that variable called pointer variable.
 Here the terms enclosed with in angular bracket are
optional. And data type and * symbol with variable
are mandatory
Syntax:
<storage_class_specifier> <Type_Qualifier>
<Type_modifier> data type * pointer variable name ;
Example:
int *a;
Pointer Declaration
L.NIVETHA AP/CSE,KNCET
Syntax:
data type * pointer variable = &normal
variable name;
Example:
int *p;
int x=20;
p = &x;
Pointer Initialization
variable value address
p 9000 6000
x 20 9000
L.NIVETHA AP/CSE,KNCET
Example program:
#include<stdio.h>
#include<conio.h>
Void main()
{
int *p , x=15 ;
p=&x;
clrscr();
printf(“the value of x is %d”, x); 15
printf(“the address of x is %u“, &x); 4000
printf(“the value of pointer is %d”, *p); 15
printf(“the address of p is %u”, &p); 4002
getch();
} L.NIVETHA AP/CSE,KNCET
Referencing operator
The reference to an object can be created
using referencing operator ( & )
It is a unary operator and it appear before the
operand.
The operands of referencing operator should
be arithmetic type.
POINTER OPERATOR
L.NIVETHA AP/CSE,KNCET
Dereferencing operator:
The object referenced by a pointer can be
indirectly accessed by dereferencing the
pointer.
A pointer can be dereferenced by using a
dereference operator ( * ),
It is a unary operator it is appear before
the dereferenced operand
L.NIVETHA AP/CSE,KNCET
Example – 2
#include<stdio.h>
void main()
{
int a=10;
int *b=&a;
printf ( "%dn“ , a);
printf ( "%un“ , &a);
printf( "%dn“ , *b);
printf( "%un“ , &b);
}
10
6487580
10 output
6487568
--------------------------------
Process exited after 0.05086 seconds with return value 8
Press any key to continue . . .
L.NIVETHA AP/CSE,KNCET
The pointer are used for pointing different
data types.
if a pointer points any data types and is
known as void pointer.
Syntax:
void *pointer variable name ;
Example:
void *a;
float x=10.0;
a = &x;
Void pointer or generic pointer
L.NIVETHA AP/CSE,KNCET
1.Null pointer
A null pointer is a special pointer does not
points anywhere.
it does not hold address of any variable ,it is
assigned by 0.
Syntax:
datatype *ptr=0;
Example:
int *a ,*b;
a=b=0;
Accessing variable through pointer
L.NIVETHA AP/CSE,KNCET
Here we using pointer variable to do any arithmetic
operation through pointer expression.
Example:
#include <stdio.h>
void main()
{
int * a ;
int * b;
int x=10 , y = 20;
a=&x;
b=&y;
printf(“%d ” , (*a+*b));
printf(“%d” , (*a - *b));
}
Pointer expression
L.NIVETHA AP/CSE,KNCET
If a pointer variable holds the address of
another pointer variable , then that pointer
variable is said to be pointer to pointer variable
Syntax:
data type **pointer to pointer variable;
Example:
int *b;
int x=10;
int **a;
b=&x;
a=&b ;
Pointer to pointer
L.NIVETHA AP/CSE,KNCET
#include<stdio.h>
#include<conio.h>
Void main()
{
int a=10;
int *b;
int **c;
b=&a;
c=&b;
printf(“the value of a is %d”, a); 10
printf(“the value of b is %d”, *b); 10
printf(“the value of c is %d”, **c); 10
printf(“the address of a is %u”, &a); 1000
printf(“the address of b is %u”, &b); 2000
printf(“the address of c is %u”, &c); 3000
}
L.NIVETHA AP/CSE,KNCET
• Arithmetic operation also possible by using
pointer variable , it is mostly performed in
restricted form.
• Addition process : *a + *b
• Subtraction process : *a - *b
• Increment process : ++ptr
• Decrement process : --ptr
Pointer Arithmetic
L.NIVETHA AP/CSE,KNCET
Addition process :
In pointer possible add two pointer values
Here we add two pointers by using ‘+’
operator .
Here we access the pointer variable by
using dereference operator ‘ *’
Example:
int *a ,*b;
int x=10 , y=20;
a=&x , b=&y;
printf(“%d” , (*a + *b));
L.NIVETHA AP/CSE,KNCET
Subtraction process :
In pointer possible to subtract two pointer
values
Here we subtract two pointers by using ‘
- ’ operator .
Here we access the pointer variable value
by using dereference operator ‘ *’
Example:
int *a ,*b;
int x=10 , y=20;
a=&x , b=&y ;
printf( “%d” , (*a - *b));
L.NIVETHA AP/CSE,KNCET
#include<stdio.h>
#include<conio.h>
Void main()
{
int *a , *b , x=10, y=20;
a=&x;
b=&y;
printf( “Addition :%d”, *a + *b);
printf( “Subtraction: %d”, *a - *b);
printf( “Multiplication: %d”, *a * *b);
getch() ;
}
L.NIVETHA AP/CSE,KNCET
• Increment and decrement operation only
performed on pointer address,
• Here output of the increment and decrement
operation is performed based on the data
type declared .
Effect of increment & increment
operator
L.NIVETHA AP/CSE,KNCET
Example program:
#include<stdio.h>
#include<conio.h>
Void main()
{
int *p , x ;
p=&x;
Clrscr();
printf(“%u”, &x); 2000
printf(“%u”, ++p); 2002
printf(“%u”, --p); 2000
printf(“%u”, p++); 2002
printf(“%u”, p--); 2000
getch();
}
L.NIVETHA AP/CSE,KNCET
It is possible to create a pointer that points to a
complete array instead of pointing to the individual
element of an array. Such a pointer is known as a
pointer to an array.
Syntax:
Datatype * array pointer variable [size] ;
Example:
Int *ptr[10]
Int x=10 , y=20;
Ptr[0]=&x
Ptr[1]=&y
Arrays of pointer
L.NIVETHA AP/CSE,KNCET
#include<stdio.h>
#include<conio.h>
Void main()
{
int a[5]={10,20,12,13,14}, c;
int *p[5]; a[5]
clrscr();
for(i=o ; i<5 ; i++)
{
p[i]= &a[i];
printf( “ %u”, p);
printf( “%d”, *p);
getch();
}
2000 10
2002 20
2004 12
2006 13
2008 14
L.NIVETHA AP/CSE,KNCET
Selection sort
0 1 2 3 4 index
A={ 1 , 3 , 5, 6 , 10}
<
5<3 T temp =
Temp=A[0]
A[0]=A[3]
A[3]=temp
L.NIVETHA AP/CSE,KNCET