You are on page 1of 10

DSM COLLEGE PARBHANI Chapter 4 : Arrays

Sr. No. Chapter 4 :- Arrays


1. Array declaration, initialization
2. Types – one, two and multidimensional,
3. What is Pointer?, Pointer declaration, initialization
4. Pointer to pointer
5. Arrays and pointers
6. Functions and pointers.

Introduction :-

➢ Array is belongs to derived data type.


➢ Array is collection of element/variable.
➢ Element/variable must have of same type.
➢ All element of array will be stored in successive memory location.
➢ Array share the common name .
➢ To identify each and every element of array a specific number is given to
them called index/subscript.
➢ On the basic of index/subscript associated with array name .
➢ They can be categorized into following types :-
1) One-dimensional array
2) Two- dimensional array
3) Multi- dimensional array

1) One-dimensional array:-

➢ The array which having only one index/subscript is called One-dimensional


array
➢ One-dimensional array sometimes called as liner array
➢ Following is the syntax to declare One-dimensional array :-
Datatype array-name[size];
➢ Here Datatype may int,char,float,double etc.
➢ Array-name is the name of array .
➢ Size indicates how many elements going to store in a array.
EX:-
Char x [5];
Int a [4];
Float per [10];

Page 1
DSM COLLEGE PARBHANI Chapter 4 : Arrays

➢ Following is the syntax to initialize the array array-name [index]=value;


a[0]=11;
a[1]=22;
a[0]=33;
a[2]=44;

➢ When above statement get executed following structure is created in


memory:-
0 1 2 3

11 22 33 44

➢ Following is the syntax to access or print the value of array element .


Array-name[index];
Ex :- printf(“%d,a[0]”);

Program :-
// program to demonstrate one d array.
#include<stdio.h>
#include<conio.h>
Void main ()
{
Int a [4];
a[0]=11;
a[1]=22;
a[0]=33;
a[2]=44;
printf(“\n %d,a[0]”);
printf(“\n %d,a[1]”);
printf(“\n %d,a[2]”);
printf(“\n%d,a[3]”);
getch();
}

Page 2
DSM COLLEGE PARBHANI Chapter 4 : Arrays

2) Two-dimensional array :-
➢ The array which having two index/subscript is called Two-dimensional
array
➢ Two -dimensional array is also called as matrix(table/tabular)
➢ Following is the syntax to declare two-dimensional array :-
Datatype array-name[size 1] [size 2];
➢ Here Datatype may int,char,float,double etc.
➢ Array-name is the name of array .
➢ Size 1 indicates how many rows.
➢ Size 2 indicates how many columns.
➢ Above statement will create following structure in memory .
A 0 1

➢ Syntax to initialize the two dimensional array .


Array-name [index 1] [index 2]=value
Ex-
a[0] [0]=55;
a[1][0]=66;
a[0][1]=77;
a[1][1]=88;

above stamements creates following strecture in memory

A 0 1

55 77
0
66 88
1

Syntax to acces /print the two diamesion array element


array-name [index1] [index2];

Page 3
DSM COLLEGE PARBHANI Chapter 4 : Arrays

ex:-
printf(“\n %d,a[0][0]”);
printf(“\n %d,a[0][1]”);
printf(“\n %d,a[1][0]”);
printf(“\n%d,a[1][1]”);

// program to demonstrate 2 diamensional array.


#include<stdio.h>
#include<conio.h>
Void main ()
{
Int a[2] [2];
a[0] [0]=55;
a[1][0]=66;
a[0][1]=77;
a[1][1]=88;
printf(“\n %d,a[0][0]”);
printf(“\n %d,a[0][1]”);
printf(“\n %d,a[1][0]”);
printf(“\n%d,a[1][1]”);
getch();
}
3) Multi- dimensional array :-

➢ The array which having more than two indexes is called multidiamemsional
array.
➢ Following is the syntax to decleare multi diamensional array :-
Datatype array-name [size 1] [size 2] [size 3]…… [size n];
Ex:-Int a[2] [2] [2];
Following is the syntax to initialize multi-dimensional array.
Array-name [index 2] [index 2] [index 3] …….[index n]=value ;
Ex:-
a [0] [0] [0] =10;
a [0] [1] [0] =20;
a [1] [0] [0] =30;
a [1] [1] [0] =40;
a [0] [0] [1] =10;
a [0] [1] [1] =20;
a [1] [0] [1] =30;
a [1] [1] [1] =40;
Page 4
DSM COLLEGE PARBHANI Chapter 4 : Arrays

pointer :-

➢ It is one kind of variable.


➢ It belongs to derive data types.
➢ It is used to store the address of another variable.
➢ To work with pointer and & *(astric) operator is used.
➢ & means address of
➢ * means value at
➢ Consider the following line of code.
int a;
➢ It will create following structure in memory
Address 1000 2 bytes

name a
a=10
will create following structure in memory.

Address 1000 2 bytes

value 10

a=10

➢ The pointer variable can be declare by using following syntax.


Datatype*pointer-variable-name;

➢ Here datatype may be int,char,float,etc.


➢ Pointer variable-name is the name of the variable which is the pointer.
➢ This pointer variable will store the address not value .
Ex:- int*p;will create following structure in memory.
Address 2000

P name

Page 5
DSM COLLEGE PARBHANI Chapter 4 : Arrays

➢ As we know pointer store only address it can be initialized by


Pointer-variable-name-=&name-of-other-variable.
Ex:-p=&a;
will create following structure in memory.
Address 2000

value 1000

➢ If you write printf(“%d”,a);will print 10.


➢ If you write statement printf(“%d”,&a);will print 1000.
➢ If you write statement printf(“%d”,&p);will print 1000.
➢ If you write statement printf(“%d”,*a);will print 10 because *p
Value at (p)
Value at(1000);
10;

// program to demonstrate pointer


#include<stdio.h>
#include<conio.h>
Void main ()
{
int a;
int *p;
clrscr();
a=10;
p=&a;
printf(“\n the value of a= %d”,a);
printf(“\n the address of a= %u”,&a);
printf(“\n the value of p =%u”,p);
printf(“\nthe address of p=%d”,*p);
printf(“\nthe address of p=%u”,&p);
getch();
}

Page 6
DSM COLLEGE PARBHANI Chapter 4 : Arrays

Pointer to pointer :-

➢ Pointer is a variable which stores the address of other variables .


➢ In c language we can points to yhe ither pointer by using pointer to pointer
variable.
int a;
a=99;

➢ pointer to pointer variable can be declare by using following syntax.


Datatype**pointer-to-pointer;
Ex- int**a

➢ As we know pointer to pointer is a special kind of variable which stores the


address of another pointer variable so it can be initialized by using following
syntax.
Pointer-to-pointer=&pointer;
Ex- a=&p
Program :-
// program to demonstrate pointer to pointer
#include<stdio.h>
#include<conio.h>
Void main ()
{
int a; //simple variable
int *p; // pointer variable
int**q;// pointer to pointer variable
a=10;
p=&a;
q=&p;

printf(“\n the value of a= %d”,a);


printf(“\n the address of a= %u”,&a);
printf(“\n the value of p =%u”,p);
printf(“\nthe address of p=%d”,*p);
printf(“\nthe address of p=%u”,&p);

Page 7
DSM COLLEGE PARBHANI Chapter 4 : Arrays

printf(“\n the value of q= %u”,a);


printf(“\nthe address of p=%u”,*a);
printf(“\n the value of p =%d”,**q);
getch();
}
Output:-
The value of a=10
The address of a=65524
The value of p =65524
The address of a=10
The address of p=65524
The value of q=65524
The address of p=65524
The value of a =10

Array and pointer:

➢ Array is a collection of elements which having same type


➢ Pointer is a variable which stores the address of another variable .
➢ These two derived data type work together for array manipulation.
➢ If you declare with four element as
int a[4];
a[0] [0]=11;
a[1]=22;
a[2]=33;
a[3]=44;
printf(“\n %d”,a[0]);
printf(“\n %d”,a[1]);
printf(“\n %d”,a[2]);
printf(“\n%d”,a[3]);
➢ Will print 11,22,33,44.
➢ If we write printf(“%d”,a); will print address of first element of array .
➢ We can use this address to manipulate array by using pointer array by using
pointer as
int*p;
p=a;
➢ If we print the value of p by using statement printf (“%d”,p); will return
address of first element of array.
➢ If wee use *(astrisk) symbol with p will print value at stored address as
Printf(“%d”,p);will return 11.
Page 8
DSM COLLEGE PARBHANI Chapter 4 : Arrays

➢ To move on further elements of array we can use ++(increment operator ) or


increment the value of by specific integer number.
➢ P++/p+1; will store the address of second element
➢ Similarly to print 3rd and 4th element address and there value
➢ Following statement will be useful.
Printf(“%d”,p+2);
Printf(“%d”,*p+2);
Printf(“%d”,p+3);
Printf(“%d”,*p+3);

// program to demonstrate array and pointer


#include<stdio.h>
#include<conio.h>
Void main ()
{
int a[]={11,22,33,44};
int *p;
p=a;
printf(“\n The address of first element=%u”,p);
printf(“\n The value of first element=%d”,*p);
printf(“\n The address of second element=%u”,p+1);
printf(“\n The value of second element=%d”,*(p+1));
printf(“\n The address of third element=%u”,p+2);
printf(“\n The value of third element=%d”,*(p+2));
printf(“\n The address of forth element=%u”,p+3);
printf(“\n The value of fourth element=%d”,*(p+3));
getch();
}

Function and pointer :-

➢ Function is a self content of a block used to perform specific task .


➢ Pointer is a variable which stores the address of other variables .
➢ We can use pointer to store address return by function
➢ To force the function to return the address instead of value use (&)operator.
➢ When we call that function it will return the address not the value.
➢ As we know the pointer will store the address then it will be used to access
the value as well as address return by function.

Page 9
DSM COLLEGE PARBHANI Chapter 4 : Arrays

Which shown below


// program to demonstrate function and pointer
#include<stdio.h>
#include<conio.h>
void main ()
int&add(int x,int y);//prototype
int&add(int x,int y);//definition
{
int z ;
z=x+y:
return z;
}
void main ()
{
int*p;
p=add(10,20);//calling
printf(“%d”,p);
printf(“%d”,*p);
fetch();
}

Page 10

You might also like