Unit 4
Unit 4
Introduction of Array
Pointer and Arrays
Array of Functions
String
Need of Array Variable
An array is defined as the collection of similar type of data items stored at contiguous
memory locations. Arrays are the derived data type in C programming language which can
store the primitive type of data such as int, char, double, float, etc.
It also has the capability to store the collection of derived data types, such as pointers,
structure, etc.
The array is the simplest data structure where each data element can be randomly
accessed by using its index number.
Advantage of C Array
2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.
Disadvantage of C Array
Fixed Size: We define the size at the time of declaration of the array, we can't exceed the
limit. So, it doesn't grow the size dynamically like LinkedList
Declaring an array
Syntax
By default array index starts with
data-type variable-name[size];
0.
Integer Array [0] [1] [2] [3] [4] If we declare an array of size 5
int mark[5]; then its index ranges from 0 to 4.
First element will be store at
integer
mark[0] and last element will be
stored at mark[4] not mark[5].
[0] [1] [2] [3] [4]
Float Array Like integer and float array we can
float avg[5]; declare array of type char.
float
Initialing and Accessing an Array
Declaring, initializing and accessing single integer variable
int mark=90; //variable mark is initialized with value 90
printf("%d",mark); //mark value printed
Declaring, initializing and accessing integer array variable
int mark[5]={85,75,76,55,45}; //mark is initialized with 5 values
printf("%d",mark[0]); //prints 85
printf("%d",mark[1]); //prints 75
printf("%d",mark[2]); //prints 65
printf("%d",mark[3]); //prints 55
printf("%d",mark[4]); //prints 45
[0] [1] [2] [3] [4]
mark[5]
85 75 65 55 45
Read(Scan) Array Elements
Reading array without loop Reading array using loop
void main() void main()
{ {
int mark[5]; int mark[5],i;
printf("Enter array element="); for(i=0;i<5;i++)
scanf("%d",&mark[0]); {
printf("Enter array element="); printf("Enter array element=");
scanf("%d",&mark[1]); scanf("%d",&mark[i]);
printf("Enter array element="); }
scanf("%d",&mark[2]); for(i=0;i<5;i++)
printf("Enter array element="); {
scanf("%d",&mark[3]); printf("%d",mark[i]);
printf("Enter array element="); }
scanf("%d",&mark[4]); }
printf("%d",mark[0]);
printf("%d",mark[1]);
printf("%d",mark[2]); [0] [1] [2] [3] [4]
printf("%d",mark[3]); mark[5]
printf("%d",mark[4]); } 85 75 65 55 45
Develop a program to count number of positive or negative number from an array of 10 Number
Program
void main(){
int num[10],i,pos,neg; Output
pos = 0; Enter array element=1
neg = 0; Enter array element=2
for(i=0;i<10;i++) Enter array element=3
{ Enter array element=4
printf("Enter array element="); Enter array element=5
scanf("%d",&num[i]); Enter array element=-1
} Enter array element=-2
for(i=0;i<10;i++) Enter array element=3
{ Enter array element=4
if(num[i]>0) Enter array element=5
pos=pos+1; Positive=8,Negative=2
else
neg=neg+1;
}
printf("Positive=%d,Negative=%d",pos,eg);}
Two dimensional Array in C
Two dimensional Array in C
Syntax
data-type variable-name[x][y]; A two dimensional array can be seen as a
table with ‘x’ rows and ‘y’ columns.
Declaration The row number ranges from 0 to (x-1) and
int data[3][3]; //This array can hold 9 elements column number ranges from 0 to (y-1).
printf("%d",data[2][0]);//7
printf("%d",data[2][1]); //8
printf("%d",data[2][2]); //9
// data[3][3] can be initialized like this also
int data[3][3]={{1,2,3},{4,5,6},{7,8,9}};
Read(Scan) 2D Array Elements
Program
void main(){
int data[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{ Output Enter array element=1
printf("Enter array element="); Enter array element=2
scanf("%d",&data[i][j]); Enter array element=3
} Enter array element=4
} Enter array element=5
for(i=0;i<3;i++) Enter array element=6
{ Enter array element=7
for(j=0;j<3;j++) Enter array element=8
{ Enter array element=9
printf("%d",data[i][j]); 1 2 3
} 4 5 6
printf("\n"); }} 7 8 9
Develop a program to count number of positive, negative and zero elements from 3 X 3 matrix
Program
Output
void main(){
Enter array element=9
int data[3][3],i,j,pos=0,neg=0,zero=0;
Enter array element=5
for(i=0;i<3;i++)
Enter array element=6
{
Enter array element=-3
for(j=0;j<3;j++)
Enter array element=-7
{
Enter array element=0
printf("Enter array element=");
Enter array element=11
scanf("%d",&data[i][j]);
Enter array element=13
if(data[i][j]>0)
Enter array element=8
pos=pos+1;
positive=6,negative=2,zero=1
else if(data[i][j]<0)
neg=neg+1;
else
zero=zero+1;
}
}
printf("positive=%d,negative=%d,zero=%d",pos,neg,zero)}
Declaration & Initialization of Pointer
Syntax
p is integer pointer variable
datatype *ptr_variablename; & is address of or referencing operator which
Example returns memory address of variable.
void main() * is indirection or dereferencing operator which
{ returns value stored at that memory address.
int a=10, *p; // assign memory address of a & operator is the inverse of * operator
to pointer variable p
p = &a;
x = a is same as x = *(&a)
printf("%d %d %d", a, *p, p);
}
Output
10 10 5000
Write a program to print variable, address of pointer variable and pointer to
pointer variable.
int **ptr;
address address value
Write a program to print variable, address of pointer variable and pointer to pointer variable.
#include <stdio.h>
int main () {
int var;
int *ptr;
int **pptr;
var = 3000;
ptr = &var; // address of var
pptr = &ptr; // address of ptr using address of operator &
printf("Value of var = %d\n", var );
printf("Value available at *ptr = %d\n", *ptr );
printf("Value available at **pptr = %d\n", **pptr);
return 0;
}
Output
Value of var = 3000
Value available at *ptr = 3000
Value available at **pptr = 3000
Relation between Array & Pointer
When we declare an array, compiler allocates continuous blocks of
memory so that all the elements of an array can be stored in that
memory.
The address of first allocated byte or the address of first element is
assigned to an array name.
Thus array name works as pointer variable.
The address of first element is also known as base address.
2018
Relation between Array & Pointer – Cont.
Example: int a[10], *p;
a[0] is same as *(a+0), a[2] is same as *(a+2) and a[i] is same as *(a+i)
Syntax
datatype *name[size];
2018
int *ptr[5]; //declares an array of integer pointer of size 5
Relation between Array & Pointer
When we declare an array, compiler allocates continuous blocks of
memory so that all the elements of an array can be stored in that
memory.
The address of first allocated byte or the address of first element is
assigned to an array name.
Thus array name works as pointer variable.
The address of first element is also known as base address.
2018
Array of Pointer – Cont.
An array of pointers ptr can be used to point to different rows of matrix as
follow:
for(i=0; i<5; i++)
{
ptr[i]=&mat[i][0];
}
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
name[10]
C O M P U T E R \0
String declaration and initialization
There are two ways to declare a Initialization method 1:
string in c language. char name[10]={‘C',‘O',‘M',‘P',‘U',‘T',‘E',’R’,'\
By char array 0'};
By string literal Initialization method 2:
Declaration char name[10]=“COMPUTER";
char name[10]; //'\0' will be automatically inserted at the end in thi
s type of declaration.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
C O M P U T E R \0
name[10]
Difference between char array and string literal
There are two main differences between char array and literal.
We need to add the null character '\0' at the end of the array by
ourself whereas, it is appended internally by the compiler in the case
of the character array.
The string literal cannot be reassigned to another set of characters
whereas, we can reassign the characters of the array.
Read String: scanf()
void main()
Output
{
char name[10]; Enter name: Parul
printf("Enter name:"); Name=Parul
scanf("%s",name); Output
printf("Name=%s",name);
Enter name: CSE Parul
}
Name=CSE
gets(): Reads characters from the standard input and stores them as a string.
puts(): Prints characters from the standard.
scanf(): Reads input until it encounters whitespace, newline or End Of File(EOF)
whereas gets() reads input until it encounters newline or End Of File(EOF).
gets(): Does not stop reading input when it encounters whitespace instead it takes
whitespace as a string.
String Handling Functions : strlen()
C has several inbuilt functions to operate on string. These functions are
known as string handling functions.
strlen(s1): returns length of a string in integer
Program
#include <stdio.h> Output
#include <string.h> //header file for string functions Enter string: Parul
void main() University
{ 15
char s1[10];
printf("Enter string:");
gets(s1);
printf("%d",strlen(s1)); // returns length of s1 in integer
}
String Handling Functions: strcmp()
strcmp(s1,s2): Returns 0 if s1 and s2 are the same.
Returns less than 0 if s1<s2.
Returns greater than 0 if s1>s2.
Program
void main() Output
{ Enter string-1:Computer
char s1[10],s2[10]; Enter string-2:Computer
printf("Enter string-1:"); Strings are same
gets(s1);
printf("Enter string-2:"); Output
gets(s2); Enter string-1:Computer
if(strcmp(s1,s2)==0) Enter string-2:Computer
printf("Strings are same"); Strings are same
else
printf("Strings are not same");}
String Handling Functions
For examples consider: char s1[]="Their",s2[]="There";
Syntax Description
strstr(s1,s2) Returns a pointer to the first occurrence of a given string s2 in string s1.
printf("%s",strstr(s1,"he"));
Output : heir
strcat(s1,s2) Appends 2nd string at the end of 1st string.
strcat(s1,s2); a copy of string s2 is appended at the end of string s1. Now s1
becomes “TheirThere”
strchr(s1,c) Returns a pointer to the first occurrence of a given character in the string s1.
printf("%s",strchr(s1,'i'));
Output : ir