UNIT-5 Concepts of Arrays and Pointer
UNIT-5 Concepts of Arrays and Pointer
Unit 5
– By Tushar Sir
– By Tushar Sir
Concepts of Single-dimensional Array
● Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
● To create an array, define the data type (like int) and specify the name of the array
followed by square brackets [].
● To insert values to it, use a comma-separated list, inside curly braces:
● It can be used to store the collection of primitive data types such as int, char, float,
etc., and also derived and user-defined data types such as pointers, structures, etc.
– By Tushar Sir
Concepts of Single-dimensional Array
● In C, we have to declare the array like any other variable before using it.
● We can declare an array by specifying its name, the type of its elements, and the size
of its dimensions.
● When we declare an array in C, the compiler allocates the memory block of the
specified size to the array name.
Syntax of Array Declaration
data_type array_name [size];
or
data_type array_name [size1] [size2]...[sizeN];
● where N is the number of dimensions.
– By Tushar Sir
Concepts of Single-dimensional Array
– By Tushar Sir
Concepts of Single-dimensional Array
Access Array Elements
● We can access any element of an array in C using the array subscript operator [ ] and
the index value i of the element.
array_name [index];
● One thing to note is that the indexing in the array always starts with 0, i.e., the first
element is at index 0 and the last element is at N – 1 where N is the number of
elements in the array.
– By Tushar Sir
Concepts of Single-dimensional Array
– By Tushar Sir
Concepts of Single-dimensional Array
Update Array Element :
● We can update the value of an element at the given index i in a similar way to
accessing an element by using the array subscript operator [ ] and assignment operator
=.
array_name[i] = new_value;
– By Tushar Sir
Concepts of Single-dimensional Array
Types of Array in C
● There are two types of arrays based on the number of dimensions it has. They are as
follows:
○ One Dimensional Arrays (1D Array)
○ Multidimensional Arrays
● The One-dimensional arrays, also known as 1-D arrays in C are those arrays that have
only one dimension.
Syntax of 1D Array in C :
array_name [size];
– By Tushar Sir
Concepts of Single-dimensional Array
– By Tushar Sir
Concepts of Single-dimensional Array
Array of Characters (Strings)
– By Tushar Sir
Concepts of Single-dimensional Array
Array of Characters (Strings)
● The null character \0 is used to find the end of characters in the array and is always
stored in the index after the last character or in the last index.
● Consider a string "character", it is indexed as the following image in a character
array.
– By Tushar Sir
Concepts of Single-dimensional Array
Array of Characters (Strings)
– By Tushar Sir
Concepts of Single-dimensional Array
– By Tushar Sir
Concepts of Single-dimensional Array
● We have 3 rows and 10 columns specified in our Array of String but because of
prespecifying, the size of the array of strings the space consumption is high.
● So, to avoid high space consumption in our program we can use an Array of Pointers
in C.
– By Tushar Sir
Rules for Declaring One Dimensional Array in C
● Before using and accessing, we must declare the array variable.
● In an array, indexing starts from 0 and ends at size-1. For example, if we have arr[10]
of size 10, then the indexing of elements ranges from 0 to 9.
● We must include data type and variable name while declaring one-dimensional arrays
in C.
● We can initialize them explicitly when the declaration specifies array size within
square brackets is not necessary.
● Each element of the array is stored at a contiguous memory location with a unique
index number for accessing.
– By Tushar Sir
Points to Remember About Array in C
● Arrays in C are a collection of similar data-type elements stored at contiguous
memory locations.
● In arrays in C, all the elements have the same data type and can be accessed by their
unique index value.
● Array indexing starts from 0 and ends with size-1.
● One-dimensional arrays in C can be initialized statically (during compile-time) or
dynamically (during runtime).
● We must include the data type, variable name for the array, and size of the array in
square brackets while declaring one-dimensional arrays in C.
– By Tushar Sir
Searching element from array (Linear Search)
● Linear Search is defined as a sequential search algorithm that starts at one end and
goes through each element of a list until the desired element is found, otherwise the
search continues till the end of the data set.
● Comparing key with first element arr[0]. SInce not equal, the iterator moves to the
next element as a potential match.
● Comparing key with next element arr[1]. SInce not equal, the iterator moves to the
next element as a potential match.
– By Tushar Sir
Searching element from array (Linear Search)
Step 2: Now when comparing arr[2] with key, the value matches. So the Linear Search
Algorithm will yield a successful message and return the index of the element when key is
found (here 2).
– By Tushar Sir
Searching element from array (Linear Search)
– By Tushar Sir
Searching element from array (Linear Search)
Advantages of Linear Search:
● Linear search can be used irrespective of whether the array is sorted or not. It can be
used on arrays of any data type.
● Does not require any additional memory.
● It is a well-suited algorithm for small datasets.
Drawbacks of Linear Search:
● Linear search has a time complexity of O(N), which in turn makes it slow for large
datasets.
● Not suitable for large arrays.
When to use Linear Search?
● When we are dealing with a small dataset.
● When you are searching for a dataset stored in contiguous memory.
– By Tushar Sir
Sorting array in ascending or descending
● Sorting an array in ascending order means arranging the elements from smallest
element to largest element.
● There are many ways by which the array can be sorted in ascending order, For
simplicity we will be using Selection Sort or we can also use Bubble Sort.
● Selection sort is a simple sorting algorithm that repeatedly finds the minimum
element from the unsorted part of the array and places it at the beginning of the sorted
part of the array until the complete array is sorted.
– By Tushar Sir
Sorting array in ascending or descending
Algorithm of Selection Sort in C :
– By Tushar Sir
Sorting array in ascending or descending
How does Selection Sort Works in C?
● Selection sort in c works by dividing the array into two parts - one that will be sorted
and the other that will be unsorted.
● After each step in the selection sort in c, we will be picking one element from the
unsorted part and put it into the sorted part of the array.
● It basically picks the smallest element from the unsorted part of the array and swaps it
with the last index of the sorted part.
● This whole process of finding the smallest element in the array from the unsorted part
and swapping it with an element of the sorted part repeats until the whole array is
sorted in either ascending or descending order.
– By Tushar Sir
Sorting array in ascending or descending
Let us take an example of how the selection sort in c
actually works: -
arr = {4,7,5,3,2,1}
– By Tushar Sir
Sorting array in ascending or descending
● In the fourth pass of the array -
○ The value of min_index = 3 and the
highest element in the rest of the array
comes out to be 4. Therefore we will
swap 4 with arr[min_index]
○ Now, the array becomes arr =
{1,2,3,4,7,5}
● In the fifth pass of the array -
○ The value of min_index = 4 and the
highest element in the rest of the array
comes out to be 5. Therefore we will
swap 5 with arr[min_index]
○ Now, the array becomes arr =
{1,2,3,4,5,7}
– By Tushar Sir
Sorting array in ascending or descending
– By Tushar Sir
Sorting array in ascending or descending
● Bubble sort in C is one of the easiest and basic sorting technique that is very easy to
implement.
● In Bubble sort in C, we compare two adjacent elements of an array to find which one
is greater or lesser and swap them based on the given condition, whether ascending or
descending, until the final place of the element is not found.
● We repeat this until no more swaps are required, and all the elements get sorted.
Bubble sort gets its name from the fact that it filters out the elements at the top of the
array-like bubbles on water.
– By Tushar Sir
Sorting array in ascending or descending
Assuming we want to sort an array in ascending order and let’s name it arr with n elements in it. Now, this
is how the Bubble sort algorithm in C will work:
● STEP 1: Starting at index zero, compare the elements with their next ones (arr[0] & arr[1]), and
swap if (arr[0]>arr[1]). After that compare arr[1] & arr[2] and swap if arr[1]>arr[2]. Repeat this
process until the end of the array.
● After that, the largest element of the array will be present at the end index. This is known as the first
pass. We have processed the array elements from [0:n−1] in this first pass.
● STEP 2: Repeat STEP 1 but process array elements from [0:n−2] because the last element arr[n-1] is
already present at its correct position. After this, the largest two elements are present at the end.
Basically, for an ith pass, we will traverse array elements till (n-i)th index because i elements from
the last are already sorted in their places.
● STEP 3: Repeat this process n-1 times to finally get the sorted array in the end. As we are getting the
largest respective element at the (n - i)th index each time, there is no need to iterate through the array
for n th pass as the first element will be the smallest element of the array already placed.
● For instance, if we pass an array consisting of the elements: (5, 3, 8, 4, 6), then the final array after
the bubble sort implementation will be: (3, 4, 5, 6, 8).
– By Tushar Sir
Sorting array in ascending or descending
– By Tushar Sir
Sorting array in ascending or descending
– By Tushar Sir
Use of \0, \n and \t
● The Escape Sequence in C is the characters or the sequence of characters that can be
used inside the string literal. The purpose of the escape sequence is to represent the
characters that cannot be used normally using the keyboard.
– By Tushar Sir
Pointers
● A pointer in C is a variable like any other variable but that holds the address of a
variable. We use them when we want to allocate some memory on the heap which is
also known as dynamic memory allocation.
● A Pointer is a variable that holds the memory address of another variable. When we
declare a variable of a specific data type we assign some of the memory for the
variable where it can store its data.
● By default the memory allocation is in stack. Using different functions(malloc in C)
or key-words(new in C++ , Java etc) we can allocate memory for a variable in the
heap section(also called as dynamic memory allocation).
● One of the major advantages of allocating memory in heap is it allows you to access
variables globally. Simply accessing the pointer that stores the address of the variable
declared in heap we can directly manipulate the data of the variable in heap.
– By Tushar Sir
Pointers
– By Tushar Sir
Pointers
Syntax:
int *p;
– By Tushar Sir
Declaring Pointers
● When we declare a pointer, it contains garbage value, which means it could be
pointing anywhere in the memory.
● Pointer Initialization is the process of assigning the address of a variable to a pointer.
● In C language, the address operator & is used to determine the address of a variable.
● The & (immediately preceding a variable name) returns the address of the variable
associated with it.
– By Tushar Sir
Declaring Pointers
– By Tushar Sir
Declaring Pointers
– By Tushar Sir
Benefits Of Pointers in C
● One of the major advantages of using pointers is dynamic memory allocation which
accelerates the program execution when we have any task of updating the variable in
different time frames as the variable is directly accessible.
● With the help of pointers we create different data structures such as Linked List ,
Trees etc.
● It also helps to return more than one function value with the help of structures and
classes.
– By Tushar Sir
Thank You
- By Tushar Sir