Unit III: Arrays and Pointers
Unit III: Arrays and Pointers
Unit III: Arrays and Pointers
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]
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
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
{ 2004 12
5<3 T temp =
Temp=A[0]
A[0]=A[3]
A[3]=temp