SlideShare a Scribd company logo
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
1
Course : PROGRAMMING IN C
TOPIC: Arrays and Pointers
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
What is an Array ?
Array is a kind of data structure that can store a fixed-size (finite) sequential
collection of elements of the same (homogeneous) type.
An array is used to store a collection of data, but it is often more useful to think of
an array as a collection of variables of the same type.
To make it simple let's break the words:
• Array is a collection - Array is a container that can hold a collection of
data.
• Array is finite - The collection of data in array is always finite, which is
determined prior to its use.
• Array is sequential - Array stores collection of data sequentially in
memory.
• Array contains homogeneous data - The collection of data in array
must share a same data type.
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
What is an Array ?
Instead of declaring individual variables, such as number0, number1, ..., and
number99, you declare one array variable such as numbers and use
numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A
specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element.
We can divide arrays in two categories.
1.One-dimensional array (Or single-dimensional array)
2.Multi-dimensional array
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Declaring Array
Declaring Arrays
To declare an array in C, a programmer specifies the type of the elements and
the number of elements required by an array as follows −
This is called a single-dimensional array. The arraySize must be an integer
constant greater than zero and type can be any valid C data type. For example,
to declare a 10-element array called balance of type double, use this statement
−
•type is a valid C data type that must be common to all array elements.
•arrayName is name given to array and must be a valid C identifier.
•arraySize is a constant value that defines array maximum capacity.
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Initializing Array
Initializing Arrays
There are two ways to initialize an array.
1.Static array initialization - Initializes all elements of array during its
declaration.
2.Dynamic array initialization - The declared array is initialized some time
later during execution of program.
Static array initialization
We define value of all array elements within a pair of curly braces { and } during
its declaration. Values are separated using comma , and must be of same type.
Note: Size of array is optional when declaring and initializing array at once. The C
compiler automatically determines array size using number of array elements. Hence,
you can write above array initialization as.
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Initializing Array
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Initializing Array
Initializing Arrays
Dynamic array initialization
You can assign values to an array element dynamically during execution of
program. First declare array with a fixed size. Then use the following syntax to
assign values to an element dynamically.
The array index is an integer value, so instead of
hard-coding you can wrap array input code inside
a loop.
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Example: Declaring / Initializing Array
Output
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Advantages/ disadvantages of Arrays ?
1. Random access of elements using array index.
2. Use of less line of code as it creates a single array of multiple elements.
3. Easy access to all the elements.
4. Traversal through the array becomes easy using a single loop.
5. Sorting becomes easy as it can be accomplished by writing less line of
code.
Advantages:
1. Allows a fixed number of elements to be entered which is decided at the
time of declaration. Unlike a linked list, an array in C is not dynamic.
2. Insertion and deletion of elements can be costly since the elements are
needed to be managed in accordance with the new memory allocation.
Disadvantages:
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Example: To find max and Min number from an array
/* * Find maximum and minimum in all
array elements. */
for(i=1; i<size; i++)
{ /* If current element is greater
than max */
if(arr[i] > max)
{
max = arr[i];
}
/* If current element is smaller
than min */
if(arr[i] < min)
{
min = arr[i];
}
} /* Print maximum and minimum
element */
printf("Maximum element = %dn",
max); printf("Minimum element =
%d", min); return 0;
}
#include <stdio.h>
#define MAX_SIZE 100
// Maximum array size
int main()
{
int arr[MAX_SIZE];
int i, max, min, size;
/* Input size of the array */
printf("Enter size of the array:
"); scanf("%d", &size);
/* Input array elements */
printf("Enter elements in the array:
"); for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
max = arr[0];
min = arr[0];
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Example: To find second largest number in an array
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Example: To find sum of all array elements
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int i, n, sum=0;
printf("Enter size of the array: "); /* Input size of the array
*/
scanf("%d", &n);
printf("Enter %d elements in the array: ", n);
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
}
/* * Add each array element to sum */
for(i=0; i<n; i++)
{
sum = sum + arr[i];
}
printf("Sum of all elements of array = %d", sum);
return 0;
}
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Multi-dimensional Array in C
Multi-dimensional array is an array of array or more precisely collection
of array. Unlike one-dimensional array, multi-dimensional array stores.
collection of array.
Let us revise the concept of dimension.
•One-dimensional array : Collection of data/values.
•Two-dimensional array : Collection of one-dimensional array.
•Three-dimensional array : Collection of two-dimensional array.
•N-dimensional array : Collection of N-1 dimensional array.
We can declare array with any dimension. However, two-dimensional
array is most popular and widely used to solve many mathematical
problems.
The simplest form of multidimensional array is the two-dimensional array.
To declare a two-dimensional integer array of size [x][y],
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Two dimensional Array in C
Where type can be any valid C data type and arrayName will be a valid C
identifier.
A two-dimensional array can be considered as a table which will have x
number of rows and y number of columns.
A two-dimensional array a, which contains three rows and four columns can
be shown as follows −
Two-dimensional array is a collection of one-dimensional array. Two-
dimensional array has special significance than other array types. We can
logically represent a two-dimensional array as a matrix. Any matrix problem
can be converted easily to a two-dimensional array.
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Two dimensional Array in C
Initializing Two-Dimensional Arrays
Multidimensional arrays may be initialized by specifying bracketed values for
each row. Following is an array with 3 rows and each row has 4 columns.
The nested braces, which indicate the intended row, are optional. The
following initialization is equivalent to the previous example −
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Two dimensional Array in C
Accessing Two-Dimensional Arrays
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Example: To Add To matrices
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Example: To multiply Two matrices
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
What is a Pointer ?
Pointers (pointer variables) are special variables that are used to store
addresses rather than values.
Synta
x
Here, we have declared a pointer p of int type which means that p can
store address of any integer value. You can also declare pointers in these
ways.
int* p;
int *p1;
int *
p2;
Let's take another example of declaring pointers.
int* p1,
p2;
Here, we have declared a pointer p1 and a normal variable p2.
Operator
Name
Description
& (address of) Returns the address of a
memory location.
* (indirection) value at (Pointer to a
variable)
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
What is a Pointer ?
Assigning addresses to Pointers
Example.
Here, 5 is assigned to the c variable. And, the address of c is assigned to
the pc pointer.
To get the value of the thing pointed by the pointers, we use
the * operator. For example
* is called the dereference operator (when working with pointers). It
operates on a pointer and gives the value stored in that pointer
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
What is a Pointer ?
Let's take one more example.
Initially, the address of c is assigned to the pc pointer using
pc=&c; Since c is 5, *pc gives us 5.
Then, the address of d is assigned to the pc pointer using
pc=&d;. Since d is -15, *pc gives us -15
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Example: Working of Pointer
Let's take one more example.
output
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Example: Working of Pointer (Explanation)
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Example: Working of Pointer (Explanation)
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Example: Working of Pointer (Explanation)
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Passing Array as a Function argument
Way-1
Formal parameters as a pointer −
Way-2
Formal parameters as a sized array −
Way-3
Formal parameters as an unsized array −
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Example: Passing Array as a Function argument
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Returning Array from a Function
C programming does not allow to return an entire array as an argument to a
function. However, you can return a pointer to an array by specifying the array's
name without an index.
If you want to return a single-dimension array from a function, you would have to
declare a function returning a pointer as in the following example
Second point to remember is that C does not advocate to return the address of
a local variable to outside of the function, so you would have to define the local
variable as static variable.
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Returning Array from a Function
consider the following function which will generate 10 random numbers and
return them using an array and call this function as follows:
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Pointer to an Array (Relation b/w Array and Pointer)
An array name is a constant pointer to the first element of the array. Therefore,
in the declaration −
balance is a pointer to &balance[0], which is the address of the first element of
the array balance. Thus, the following program fragment assigns p as the
address of the first element of balance −
It is legal to use array names as constant pointers, and vice versa. Therefore,
*(balance + 4) is a legitimate way of accessing the data at balance[4].
Once you store the address of the first element in 'p', you can access the array
elements using *p, *(p+1), *(p+2) and so on. Given below is the example to show
all the concepts discussed above −
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
In the above example, p is a pointer
to double, which means it can store
the address of a variable of double
type. Once we have the address in
p, *p will give us the value available
at the address stored in p, as we
have shown in the above example.
Pointer to an Array (Relation b/w Array and Pointer)
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Modules based upon array and pointers
1. Write suitable array declarations for the following:
a)100 items of integer type
int item[100];
b) A string of 25 characters
char [26];
c) A matrix of order 5 x4
int mat[5][4];
2. Find the output
void main()
{ int a=10, *p, **q;
p=&a; q=&p;
printf(“%d %d %u %u %u”, *p,**q,p,q,*q);
}
Output::
10 10 3616071484 3616071488 3616071484
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Modules based upon array and pointers
3. Find the output
a) void main()
{
int i, x[5]={1,2,3,4,5};
for(i=0;i<20;i++)
printf(“n%d”,x[i]);
}
4. Find the output
b) void main()
{
unsigned char var=0;
for(;var>=0;i++)
printf(“n%d”,var);
}
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Modules based upon array and pointers
5. Find the output
a) void main()
{
int i=0;
for(;i<9;)
{ i++;
printf(“n%d”,i);
}
}
6. Find the output
b) void fun()
{ if(x>0)
fun(--x);
printf(“n%d”,x);
}
void main()
{ fun(5);
}
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Modules based upon array and pointers
7. Find the output
a) void main()
{
int num, *p;
num=5;
p=&num;
printf(“%d”,*p);
}
8. Find the output
b) void main()
{ int I;
static float arr[ ]={1.2,16,34,4.5,45};
for(i=0;i<5;i++)
printf(“n%f”,arr[i]);
}
UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
THANK YOU
Ad

Recommended

Arrays.pptx
Arrays.pptx
PankajKumar497975
 
arrays.docx
arrays.docx
lakshmanarao027MVGRC
 
Chapter 13.pptx
Chapter 13.pptx
AnisZahirahAzman
 
Unit4pptx__2024_11_ 11_10_16_09.pptx
Unit4pptx__2024_11_ 11_10_16_09.pptx
GImpact
 
Programming in c Arrays
Programming in c Arrays
janani thirupathi
 
Arrays
Arrays
VenkataRangaRaoKommi1
 
Programming in c arrays
Programming in c arrays
Uma mohan
 
Arrays
Arrays
swathi reddy
 
Array and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdf
ajajkhan16
 
Arrays
Arrays
Steven Wallach
 
Arrays & Strings
Arrays & Strings
Munazza-Mah-Jabeen
 
Abir ppt3
Abir ppt3
abir96
 
Arrays
Arrays
ViniVini48
 
Array
Array
hjasjhd
 
Arrays In C Language
Arrays In C Language
Surbhi Yadav
 
3.ArraysandPointers.pptx
3.ArraysandPointers.pptx
FolkAdonis
 
Arrays.pptx
Arrays.pptx
NavyaParashir
 
Array
Array
PralhadKhanal1
 
Array in C
Array in C
adityas29
 
ARRAYS IN C.pptx
ARRAYS IN C.pptx
Liga8
 
Arrays in c
Arrays in c
Jeeva Nanthini
 
Array ppt you can learn in very few slides.
Array ppt you can learn in very few slides.
dwivedyp
 
Arrays in c language
Arrays in c language
tanmaymodi4
 
Arrays in c v1 09102017
Arrays in c v1 09102017
Tanmay Modi
 
Arraysincv109102017 180831194256
Arraysincv109102017 180831194256
ABHAY9616302301
 
Array
Array
HarshKumar943076
 
Arrays Basics
Arrays Basics
Nikhil Pandit
 
Array&amp;string
Array&amp;string
chanchal ghosh
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 

More Related Content

Similar to 10 arrays and pointers.pptx for array and pointer (20)

Array and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdf
ajajkhan16
 
Arrays
Arrays
Steven Wallach
 
Arrays & Strings
Arrays & Strings
Munazza-Mah-Jabeen
 
Abir ppt3
Abir ppt3
abir96
 
Arrays
Arrays
ViniVini48
 
Array
Array
hjasjhd
 
Arrays In C Language
Arrays In C Language
Surbhi Yadav
 
3.ArraysandPointers.pptx
3.ArraysandPointers.pptx
FolkAdonis
 
Arrays.pptx
Arrays.pptx
NavyaParashir
 
Array
Array
PralhadKhanal1
 
Array in C
Array in C
adityas29
 
ARRAYS IN C.pptx
ARRAYS IN C.pptx
Liga8
 
Arrays in c
Arrays in c
Jeeva Nanthini
 
Array ppt you can learn in very few slides.
Array ppt you can learn in very few slides.
dwivedyp
 
Arrays in c language
Arrays in c language
tanmaymodi4
 
Arrays in c v1 09102017
Arrays in c v1 09102017
Tanmay Modi
 
Arraysincv109102017 180831194256
Arraysincv109102017 180831194256
ABHAY9616302301
 
Array
Array
HarshKumar943076
 
Arrays Basics
Arrays Basics
Nikhil Pandit
 
Array&amp;string
Array&amp;string
chanchal ghosh
 
Array and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdf
ajajkhan16
 
Abir ppt3
Abir ppt3
abir96
 
Arrays In C Language
Arrays In C Language
Surbhi Yadav
 
3.ArraysandPointers.pptx
3.ArraysandPointers.pptx
FolkAdonis
 
ARRAYS IN C.pptx
ARRAYS IN C.pptx
Liga8
 
Array ppt you can learn in very few slides.
Array ppt you can learn in very few slides.
dwivedyp
 
Arrays in c language
Arrays in c language
tanmaymodi4
 
Arrays in c v1 09102017
Arrays in c v1 09102017
Tanmay Modi
 
Arraysincv109102017 180831194256
Arraysincv109102017 180831194256
ABHAY9616302301
 

Recently uploaded (20)

FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Introduction to problem solving Techniques
Introduction to problem solving Techniques
merlinjohnsy
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Introduction to problem solving Techniques
Introduction to problem solving Techniques
merlinjohnsy
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Ad

10 arrays and pointers.pptx for array and pointer

  • 1. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org 1 Course : PROGRAMMING IN C TOPIC: Arrays and Pointers
  • 2. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org What is an Array ? Array is a kind of data structure that can store a fixed-size (finite) sequential collection of elements of the same (homogeneous) type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. To make it simple let's break the words: • Array is a collection - Array is a container that can hold a collection of data. • Array is finite - The collection of data in array is always finite, which is determined prior to its use. • Array is sequential - Array stores collection of data sequentially in memory. • Array contains homogeneous data - The collection of data in array must share a same data type.
  • 3. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org What is an Array ? Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. We can divide arrays in two categories. 1.One-dimensional array (Or single-dimensional array) 2.Multi-dimensional array
  • 4. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Declaring Array Declaring Arrays To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows − This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can be any valid C data type. For example, to declare a 10-element array called balance of type double, use this statement − •type is a valid C data type that must be common to all array elements. •arrayName is name given to array and must be a valid C identifier. •arraySize is a constant value that defines array maximum capacity.
  • 5. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Initializing Array Initializing Arrays There are two ways to initialize an array. 1.Static array initialization - Initializes all elements of array during its declaration. 2.Dynamic array initialization - The declared array is initialized some time later during execution of program. Static array initialization We define value of all array elements within a pair of curly braces { and } during its declaration. Values are separated using comma , and must be of same type. Note: Size of array is optional when declaring and initializing array at once. The C compiler automatically determines array size using number of array elements. Hence, you can write above array initialization as.
  • 6. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Initializing Array
  • 7. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Initializing Array Initializing Arrays Dynamic array initialization You can assign values to an array element dynamically during execution of program. First declare array with a fixed size. Then use the following syntax to assign values to an element dynamically. The array index is an integer value, so instead of hard-coding you can wrap array input code inside a loop.
  • 8. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: Declaring / Initializing Array Output
  • 9. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Advantages/ disadvantages of Arrays ? 1. Random access of elements using array index. 2. Use of less line of code as it creates a single array of multiple elements. 3. Easy access to all the elements. 4. Traversal through the array becomes easy using a single loop. 5. Sorting becomes easy as it can be accomplished by writing less line of code. Advantages: 1. Allows a fixed number of elements to be entered which is decided at the time of declaration. Unlike a linked list, an array in C is not dynamic. 2. Insertion and deletion of elements can be costly since the elements are needed to be managed in accordance with the new memory allocation. Disadvantages:
  • 10. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: To find max and Min number from an array /* * Find maximum and minimum in all array elements. */ for(i=1; i<size; i++) { /* If current element is greater than max */ if(arr[i] > max) { max = arr[i]; } /* If current element is smaller than min */ if(arr[i] < min) { min = arr[i]; } } /* Print maximum and minimum element */ printf("Maximum element = %dn", max); printf("Minimum element = %d", min); return 0; } #include <stdio.h> #define MAX_SIZE 100 // Maximum array size int main() { int arr[MAX_SIZE]; int i, max, min, size; /* Input size of the array */ printf("Enter size of the array: "); scanf("%d", &size); /* Input array elements */ printf("Enter elements in the array: "); for(i=0; i<size; i++) { scanf("%d", &arr[i]); } max = arr[0]; min = arr[0];
  • 11. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: To find second largest number in an array
  • 12. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: To find sum of all array elements #include <stdio.h> #define MAX_SIZE 100 int main() { int arr[MAX_SIZE]; int i, n, sum=0; printf("Enter size of the array: "); /* Input size of the array */ scanf("%d", &n); printf("Enter %d elements in the array: ", n); for(i=0; i<n; i++) { scanf("%d", &arr[i]); } /* * Add each array element to sum */ for(i=0; i<n; i++) { sum = sum + arr[i]; } printf("Sum of all elements of array = %d", sum); return 0; }
  • 13. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Multi-dimensional Array in C Multi-dimensional array is an array of array or more precisely collection of array. Unlike one-dimensional array, multi-dimensional array stores. collection of array. Let us revise the concept of dimension. •One-dimensional array : Collection of data/values. •Two-dimensional array : Collection of one-dimensional array. •Three-dimensional array : Collection of two-dimensional array. •N-dimensional array : Collection of N-1 dimensional array. We can declare array with any dimension. However, two-dimensional array is most popular and widely used to solve many mathematical problems. The simplest form of multidimensional array is the two-dimensional array. To declare a two-dimensional integer array of size [x][y],
  • 14. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Two dimensional Array in C Where type can be any valid C data type and arrayName will be a valid C identifier. A two-dimensional array can be considered as a table which will have x number of rows and y number of columns. A two-dimensional array a, which contains three rows and four columns can be shown as follows − Two-dimensional array is a collection of one-dimensional array. Two- dimensional array has special significance than other array types. We can logically represent a two-dimensional array as a matrix. Any matrix problem can be converted easily to a two-dimensional array.
  • 15. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Two dimensional Array in C Initializing Two-Dimensional Arrays Multidimensional arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row has 4 columns. The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to the previous example −
  • 16. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Two dimensional Array in C Accessing Two-Dimensional Arrays
  • 17. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: To Add To matrices
  • 18. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: To multiply Two matrices
  • 19. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org What is a Pointer ? Pointers (pointer variables) are special variables that are used to store addresses rather than values. Synta x Here, we have declared a pointer p of int type which means that p can store address of any integer value. You can also declare pointers in these ways. int* p; int *p1; int * p2; Let's take another example of declaring pointers. int* p1, p2; Here, we have declared a pointer p1 and a normal variable p2. Operator Name Description & (address of) Returns the address of a memory location. * (indirection) value at (Pointer to a variable)
  • 20. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org What is a Pointer ? Assigning addresses to Pointers Example. Here, 5 is assigned to the c variable. And, the address of c is assigned to the pc pointer. To get the value of the thing pointed by the pointers, we use the * operator. For example * is called the dereference operator (when working with pointers). It operates on a pointer and gives the value stored in that pointer
  • 21. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org What is a Pointer ? Let's take one more example. Initially, the address of c is assigned to the pc pointer using pc=&c; Since c is 5, *pc gives us 5. Then, the address of d is assigned to the pc pointer using pc=&d;. Since d is -15, *pc gives us -15
  • 22. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: Working of Pointer Let's take one more example. output
  • 23. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: Working of Pointer (Explanation)
  • 24. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: Working of Pointer (Explanation)
  • 25. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: Working of Pointer (Explanation)
  • 26. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Passing Array as a Function argument Way-1 Formal parameters as a pointer − Way-2 Formal parameters as a sized array − Way-3 Formal parameters as an unsized array −
  • 27. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: Passing Array as a Function argument
  • 28. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Returning Array from a Function C programming does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index. If you want to return a single-dimension array from a function, you would have to declare a function returning a pointer as in the following example Second point to remember is that C does not advocate to return the address of a local variable to outside of the function, so you would have to define the local variable as static variable.
  • 29. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Returning Array from a Function consider the following function which will generate 10 random numbers and return them using an array and call this function as follows:
  • 30. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Pointer to an Array (Relation b/w Array and Pointer) An array name is a constant pointer to the first element of the array. Therefore, in the declaration − balance is a pointer to &balance[0], which is the address of the first element of the array balance. Thus, the following program fragment assigns p as the address of the first element of balance − It is legal to use array names as constant pointers, and vice versa. Therefore, *(balance + 4) is a legitimate way of accessing the data at balance[4]. Once you store the address of the first element in 'p', you can access the array elements using *p, *(p+1), *(p+2) and so on. Given below is the example to show all the concepts discussed above −
  • 31. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org In the above example, p is a pointer to double, which means it can store the address of a variable of double type. Once we have the address in p, *p will give us the value available at the address stored in p, as we have shown in the above example. Pointer to an Array (Relation b/w Array and Pointer)
  • 32. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Modules based upon array and pointers 1. Write suitable array declarations for the following: a)100 items of integer type int item[100]; b) A string of 25 characters char [26]; c) A matrix of order 5 x4 int mat[5][4]; 2. Find the output void main() { int a=10, *p, **q; p=&a; q=&p; printf(“%d %d %u %u %u”, *p,**q,p,q,*q); } Output:: 10 10 3616071484 3616071488 3616071484
  • 33. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Modules based upon array and pointers 3. Find the output a) void main() { int i, x[5]={1,2,3,4,5}; for(i=0;i<20;i++) printf(“n%d”,x[i]); } 4. Find the output b) void main() { unsigned char var=0; for(;var>=0;i++) printf(“n%d”,var); }
  • 34. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Modules based upon array and pointers 5. Find the output a) void main() { int i=0; for(;i<9;) { i++; printf(“n%d”,i); } } 6. Find the output b) void fun() { if(x>0) fun(--x); printf(“n%d”,x); } void main() { fun(5); }
  • 35. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Modules based upon array and pointers 7. Find the output a) void main() { int num, *p; num=5; p=&num; printf(“%d”,*p); } 8. Find the output b) void main() { int I; static float arr[ ]={1.2,16,34,4.5,45}; for(i=0;i<5;i++) printf(“n%f”,arr[i]); }
  • 36. UNIT-III Arrays and Pointers W3schools.in, Tutorialspoint.com Geeksforgeeks.org, codeforwin.org THANK YOU