Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit III: Arrays and Pointers

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 46

Unit III

Arrays and Pointers


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.
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
K.ANGURAJU AP /CSE - KNCET
Types of Array
Array can be classified into:
• One dimensional Array
• Two dimensional Array
• Multi dimensional Array

K.ANGURAJU 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.
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]

K.ANGURAJU 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

K.ANGURAJU 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;
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]);
}
} K.ANGURAJU 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] );
}
K.ANGURAJU 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] );
}
} K.ANGURAJU AP /CSE - KNCET
#include<stdio.h>
void main()
{
int mark[5],i,total=0, avg;
printf("Enter your mark");
for(i=0;i<5;i++)
{
scanf("%d", &mark[i]);
}
printf("Your mark is");
for(i=0;i<5;i++)
{
printf("%d\n", mark[i]);
}
total = mark[0]+mark[1]+mark[2]+mark[3]+mark[4];
for(i=0;i<5;i++)
{
total=total+mark[i];
}

avg=total/5;
printf("TOTAL:%d\nAVERAGE:%d", total, avg);

}
#include<stdio.h>
void main()
{
int rollno[5] , i, srollno ;
Printf("Enter the rollno");
for(i=0; i<5; i++)
{
scanf("%d", &rollno[i]);
}
printf("Enter the srollno");
scanf("%d", &srollno);
for(i=0;i<5;i++)
{
if(srollno==rollno[i])
{
printf("Element is found");
break;
}
}
printf("element not found");
}
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]
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.
Declaring two dimensional array
 To declare the two dimensional array with two
subscript.
 First subscript with row size and 2nd subscript
with column size.
 The terms enclosed within angular brackets
(<>) are optional.
 data type and identifier with row and column
subscript are mandatory.
Syntax:
<storage_class_specifier> <Type_modifier> <type
Qualifier> data_type array_name[row size]
[column size];
Example:
int a[3][3];
col0 col1 col2
Row0 A[0][0] A[0][1] A[0][2]

Row1 A[1][0] A[1][1] A[1][2]

Row2 A[2][0] A[2][1] A[2][2]

K.ANGURAJU 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]={23,34,34,34};
K.ANGURAJU 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”);
}
} K.ANGURAJU AP /CSE - KNCET
#include<stdio.h>
Void main()
{
int a[2][2], i, j ;
printf(“Enter a matrix”);
for(i=0 ; i<2 ; i++)
{
for(j=0 ; j<2 ; j++)
{
scanf( “ %d ” , &a[i][j]);
}
}
for(i=0 ; i<2 ; i++)
{
for(j=0 ; j<2 ; j++)
{
printf( “%d”, a[i][j]);
}
}
0 1
0 2 4
A=
1 6 8

a[0][0]
int A[2][2]={
{2,4},
{6,8}
}
A[0][1]
A[1][0]
A[1][1]
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.
Declaring three dimensional array:
 Here the data type and array variable with
plane_specifier, row and column_specifier are
mandatory
 plane, row and column specifier are compile time
constant, it must be greater than zero.
Syntax:
<storage_class_specifier> <Type_modifier> <type
Qualifier> data_type
array_variable[plane_specifier][row_specifier]
[column_specifier];
Example
Int a[2][5][3];
Int a;
a= 15;
Int *b;
b=&a;
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.
Advantages of pointer
 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.
Pointer Declaration
 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 Initialization
Syntax:
data type * pointer variable = &normal
variable name;

Example:
variable value address
int *p; p 9000 6000

int x=20; x 20 9000

p = &x;
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();
}
POINTER OPERATOR
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.
.
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
Example – 2
#include<stdio.h>
void main()
{
int a=10;
int *b=&a;
printf ( "%d\n“ , a);
printf ( "%u\n“ , &a);
printf( "%d\n“ , *b);
printf( "%u\n“ , &b);
}
10
6487580
10 output
6487568
--------------------------------
Process exited after 0.05086 seconds with return value 8
Press any key to continue . . .
Void pointer or generic pointer
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;
Accessing variable through pointer
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;
Pointer expression

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 to pointer
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 ;
#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
}
Pointer Arithmetic
• 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
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));
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));
#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() ;
}
Effect of increment & increment operator

• 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 .
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();
}
Arrays of pointer
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
#include<stdio.h>
#include<conio.h>
Void main()
{
int a[5]={10,20,12,13,14}, c;
int *p[5]; a[5]
clrscr(); 2000 10

for(i=o ; i<5 ; i++) 2002 20

{ 2004 12

p[i]= &a[i]; 2006 13


printf( “ %u”, p); 2008 14
printf( “%d”, *p);
getch();
}
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

You might also like