SlideShare a Scribd company logo
Pointers and Arrays
S.Bhuvaneshwari
Assistant Professor/CSE
Sri Eshwar College of Engineering
Coimbatore
No of similar data items grouped together to form a
single entity is called as an Array.
Syntax:
datatype arrayName[subscript]
* Subscript or number of elements in an array.
Eg:
int a,b,c; can be converted as
int a[2]; (i.e) a[0], a[1], a[2]
a[0]
Initialization:
int a[2] = {1,2,3};
index
Memory Allocation:
If a[0], a[1], a[2] represent the elements of an array what does array
name represent?
ArrayName holds the base address at which the array starts.
23456
23457
23458
23459
23460
23461
23462
………..
a[0]
a[1]
a[2]
Base address at which the array starts. Array
Name holds this address. So a=23456
The data type
is int, hence
each element
is allocated 2
bytes.
ArrayName applied
with index will
retrieve the value of
the array elements.
 As the arrayName holds the base address of the array, it acts
as a pointer.
 ArrayName is a constant pointer. (i.e) it can always point to
hold the same base address.
 Bound checking will not be done for arrays in C. Garbage
value or unknown value will be returned for the Array out of
range retrieval.
Eg:
int a[10], b[10];
a and b holds the base address of their corresponding array.
Below statements are trying to change the base address ‘a’
pointing to. Hence not allowed.
a=b; // illegal.
a++; // illegal.
a = a+2; // illegal.
Difference between pointer and an array.
Pointer ArrayName
Pointer value can be changed. (i.e)
Address the pointer holds.
Array Name is a constant pointer.
Can be made to point to any value’s
address.
Points to same base address always.
Sizeof (pointer) will be always same.
(Mostly 2 bytes).
Sizeof (arrayName) varies according to
the datatype.
Sizeof (pointer) will give the size of only
the pointer.
Sizeof (arrayName) gives the size of the
whole array.
 Pointer arithmetic is applicable to arrays also.
 Just as pointer increment or decrement, arrayName
also can be incremented or decremented. Only the
control will be moved. The address to which the
arrayName points to cannot be changed.
 Pointer and arrayName are interchangeable.
 Pointer –Pointer is applicable to arrays.
 If ptr1 and ptr2 are the two pointer variables, then
ptr1-ptr2= (ptr1 – ptr2)
sizeof(data type)
Example program where arrayName acts as a pointer.
 No value is assigned for element a[2], so zero will be assigned for it.
void main()
{
int i, a[3]={1,2};
clrscr();
printf("Base Address of array:%un", a);
for(i=0;i<=2;i++)
printf("Address of element %d:%u and value :%dn", i, (a+i), *(a+i));
getch();
}
Output:
Expression Equivalent Value displayed
arrayName
(Base Address +0)
&(arrayName[0]) Base Address of the array
arrayName + 1
(Base Address +
(1 X sizeof(datatype))
&(arrayName[1]) Address of the second
element of the array
arrayName + 2
(Base Address +
(2 X sizeof(datatype))
&(arrayName[2]) Address of the third
element of the array
Expression Equivalent Value displayed
*arrayName (arrayName[0]) Value of the first element
of the array
*(arrayName + 1) (arrayName[1]) Value of the second
element of the array
*(arrayName + 2) (arrayName[2]) Value of the third element
of the array
 ArrayName holds the base address of the array, hence it
can be assigned to a pointer and can be used for Array
processing.
 Auto increment or decrement can be done in the pointer
variable.
 Array name without the brackets is the pointer name
and on the other end, a pointer can be indexed as if its
an array.
int a[10], *b;
b=a // b holds the base address of array ‘a’.
a[1] will give second element of the array.
b[1] will also give 2nd
element of the array.
Here pointer ‘b’ acts as arrayName.
Example program to process array using pointer(auto increment):
void main()
{
int iCount; /* Loop Counter */
int iaAr[5]={1,2,3,4,5}; /* Array of 5 integers */
int *piArr=iaAr; /* Pointer to array */
for(iCount=0;iCount<5;iCount++)
{ /* Print the content of array using the pointer */
printf("n%d",*piArr);
piArr++; // This auto increment cannot be done using arrayName
}
}
Example program to process array using pointer:
void main()
{
int iCount; /* Loop Counter */
int iaAr[5]={1,2,3,4,5}; /* Array of 5 integers */
int *piArr=iaAr; /* Pointer to array */
for(iCount=0;iCount<5;iCount++)
{ /* Print the content of array using the pointer */
printf("n %d",*(piArr+iCount));
}
}
Output:
Example program to process array using pointer:
void main()
{
int iCount; /* Loop Counter */
int iaAr[5]={1,2,3,4,5}; /* Array of 5 integers */
int *piArr=iaAr; /* Pointer to array */
for(iCount=0;iCount<5;iCount++)
{ /* Print the content of array using the pointer */
printf("n %d“, piArr[iCount]); // Indexing using pointer
}
}
Output:
 Indirection (or) dereference on a pointer will retrieve the value
stored in that address.
 A pointer can point to another pointer.
 The concept will be same but the application of dereference
operator will vary. As a result many layers of pointer can be
formed and this called multiple indirection.
 As such there is no limitation on the level of indirection but
including great depth of indirection is difficult to follow and may
lead to errors. Normally more than 2 level depth indirection is not
required.
 A pointer to a pointer has declaration is similar to that of a normal
pointer but have more asterisk sign before them indicating the
depth of the pointer.
Declaration:
int ** pointer;
The above statement creates a pointer which points to pointer to a
variable with int value.
int i=10;
10
int *p = &i;
23456
int **ptr = &p;
34567
23456 23457
34567 34568
4456844567
ptr p i
*ptr will retrieve the value
stored in the address of ‘p’. It is
also an address. To retrieve the
value stored in the address ‘p’
is pointing to one more
indirection is applied on *ptr.
**ptr will retrieve the value 10.
Expression Value
ptr 34567
*ptr 23456
**ptr 10
Example program for MultipleIndirection:
void main ()
{
int i = 10;
int **p1;
int *p2;
clrscr();
p2 = &i;
p1 = &p2; /* Multiple indirection */
printf (" **p1 = %d And *p2 = %d", **p1,*p2);
getch();
}
Output:
Types of arrays:
1-Dimensional array (used in previous slides)
2-Dimensional array
3-Dimensional array and so on
1-D Array:
Consists of many elements. Single row. Multiple columns.
2-D Array:
Consists of many 1-D Arrays. Multiple rows and columns.
3-D Array:
Consists of many 2-D Arrays.
Memory Allocation for a 2-D array:
Even though the 2-D array is seen as rows and columns, the memory will
be allocated continuously.
Declaration:
Datatype arrayName[rows][columns];
Rows – No Of 1-D arrays .
Columns – No Of elements in each 1-D Array.
Eg:
int a[10], b[10], c[10];
The above three arrays can be combined together to form a single array.
This forms a 2-D array with rows and columns.
int a[3][10];
Here a[0], a[1], a[2] are three 1-D arrays with 10 elements in each.
Sizeof 2-D Array:
No of elements in 2-D Array = Rows X Columns
Size of 2-D Array = No of elements X sizeof(datatype)
Initialization:
int a[3][2]={1,2,3,4,5,6}; // Elements can be given continuously.
int a[3][2]={ {1,2}, // Elements given for each 1- D Array.
{3,4},
{5,6} };
int a[ ][2] = {1,2,3,4,5,6};
No Of Rows = No of Elements / No of columns
10000 10001
10004 10005
10008 10009
1
3
5
10002 10003
10006 10007
10010 10011
2
4
6
a[0]
a[1]
a[2]
2-D Array ‘a’
Notes:
 Like 1-D Array, 2-D arrayName is also a constant pointer.
 In the previous example, 2-D arrayName ‘a’ is a constant
pointer. It holds the base address of 2-D Array.
 a[0], a[1], a[2] represent 1-D Arrays. Hence they hold the base
address of each 1-D array correspondingly.
 a[0][0], a[0][1], a[1][0], a[1][1], a[2][0], a[2][1] represent the
individual elements. They hold the values.
Example that illustrates the 1-d Array in each 2-D array:
void main()
{
int iCount, iColCount; /* Loop Counter */
int iaAr[3][2]={1,2,3,4,5,6}; /* Array of 6 integers */
clrscr();
for( iCount=0;iCount<3;iCount++)
{
for( iColCount=0;iColCount<2;iColCount++)
{
printf("Address of element iaAr[%d][%d]: %un", iCount, iColCount,
&(iaAr [iCount] [iColCount])); /* Address of each element*/
}
}
for( iCount=0; iCount<3; iCount++)
{
printf("Base Address of 1-D Array iaAr[%d]: %u n", iCount, iaAr[iCount]);
}
getch();
}
Output:
Expression Equivalent Value displayed
arrayName
(or)
*arrayName
(arrayName[0]) Base Address of the 2-D
Array (or) Base Address of
the first 1-D Array
arrayName + 1 (or)
*(arrayName+1)
(arrayName[1]) Base Address of the
second 1-D Array
arrayName + 2 (or)
*(arrayName+2)
(arrayName[2]) Base Address of the third
1-D Array
Expression Equivalent Value displayed
**arrayName (arrayName[0][0]) Value of the first element
of the first 1-D array
**(arrayName + 1) (arrayName[1][0]) Value of the first element
of the second 1-D array
**(arrayName + 2) (arrayName[2][0]) Value of the first element
of the third 1-D array
In simple way,
arrayName[row][column]
Each row (1-D Array) can be retrieved using, *(arrayName + rowNumber)
Each element in each row can be retrieved using,
*(*(arrayName + rowNumber)+ columnNumber)
Expression Equivalent Value displayed
*(*arrayName+0)+1) (arrayName[0][1]) Value of the second
element of the first 1-D
array
*(*(arrayName + 1)+1) (arrayName[1][1]) Value of the second
element of the second 1-
D array
*(*(arrayName + 2)+1) (arrayName[2][1]) Value of the second
element of the third 1-D
array
Example to display 2-D Array using index:
void main()
{
int iRowCount, iColCount; /* Loop Counter */
int iaAr[3][2]={1,2,3,4,5,6}; /* Array of 6 integers */
clrscr();
for( iRowCount=0;iRowCount<3;iRowCount++)
{
for(iColCount=0;iColCount<2;iColCount++)
{
printf("Value of element iaAr[%d][%d]: %dn", iRowCount, iColCount,
iaAr[ iRowCount][iColCount]);
}
}
getch();
}
Output:
Example to display 2-D Array using arrayName as pointer:
void main()
{
int iRowCount, iColCount; /* Loop Counter */
int iaAr[3][2]={1,2,3,4,5,6}; /* Array of 6 integers */
clrscr();
for(iRowCount=0;iRowCount<3;iRowCount++){
for(iColCount=0;iColCount<2;iColCount++){
printf("Value of element iaAr[%d][%d]: %dn", iRowCount, iColCount,
*(*(iaAr+ iRowCount)+iColCount));
} }
printf("n");
for(iRowCount=0;iRowCount<3;iRowCount++) {
for(iColCount=0;iColCount<2;iColCount++) {
printf("Value of element iaAr[%d][%d]: %dn", iRowCount, iColCount,
*(iaAr[iRowCount]+iColCount));
} }
getch();
}
Output:
Notes:
 In the same way, 2-D array is processed, 3-D, 4-D etc., arrays can also be
processed.
 3-D array is formed by combining multiple 2-D Arrays.
 Indirection(*) first applied on the arrayName, will retrieve the 2-D
array’s base address. Second indirection will retrieve the 1-D array’s base
address. Third indirection will retrieve the element value.
 Hence according to the dimension, the number of indirection operators
to be applied to retrieve the value will vary.
Eg:
int a[2][3][4];
To retrieve the value of element a[0][1][3] using arrayName as pointer,
*(*(*(a+0)+1)+3) has to be used.
 No of Elements = 2X3X4 = 24
 Sizeof(a) = 24X2 = 48 bytes

More Related Content

What's hot

If else statement in c++
If else statement in c++If else statement in c++
If else statement in c++
Bishal Sharma
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
Dr-Dipali Meher
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
Abu Bakr Ramadan
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
Tasnima Hamid
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
RacksaviR
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
Shubham Vishwambhar
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
MohammadSalman129
 
Implementation Of String Functions In C
Implementation Of String Functions In CImplementation Of String Functions In C
Implementation Of String Functions In C
Fazila Sadia
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
tanmaymodi4
 
Union In language C
Union In language CUnion In language C
Union In language C
Ravi Singh
 
Linked list
Linked listLinked list
Linked list
Md. Afif Al Mamun
 
This pointer
This pointerThis pointer
This pointer
Kamal Acharya
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
C pointer
C pointerC pointer
c-programming
c-programmingc-programming
c-programming
Zulhazmi Harith
 
Pointer in c
Pointer in cPointer in c
Pointer in c
Imamul Kadir
 
Binary trees
Binary treesBinary trees
Binary trees
Amit Vats
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
swapnac12
 
Functions in c
Functions in cFunctions in c
Functions in c
sunila tharagaturi
 

What's hot (20)

If else statement in c++
If else statement in c++If else statement in c++
If else statement in c++
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
Implementation Of String Functions In C
Implementation Of String Functions In CImplementation Of String Functions In C
Implementation Of String Functions In C
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 
Union In language C
Union In language CUnion In language C
Union In language C
 
Linked list
Linked listLinked list
Linked list
 
This pointer
This pointerThis pointer
This pointer
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
C pointer
C pointerC pointer
C pointer
 
c-programming
c-programmingc-programming
c-programming
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Binary trees
Binary treesBinary trees
Binary trees
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
 
Functions in c
Functions in cFunctions in c
Functions in c
 

Similar to Pointers and arrays

SP-First-Lecture.ppt
SP-First-Lecture.pptSP-First-Lecture.ppt
SP-First-Lecture.ppt
FareedIhsas
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
George Erfesoglou
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
HimanshuKansal22
 
Unit3 C
Unit3 C Unit3 C
Unit3 C
arnold 7490
 
Array
ArrayArray
Array
Radha Rani
 
Array assignment
Array assignmentArray assignment
Array assignment
Ahmad Kamal
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
YOGESH SINGH
 
Engineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxEngineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptx
happycocoman
 
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointer
Nishant Munjal
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
Rakesh Roshan
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
Kashif Nawab
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
Rabin BK
 
Pointer in C
Pointer in CPointer in C
Pointer in C
bipchulabmki
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
madan reddy
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
guest91d2b3
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
SamQuiDaiBo
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
DeveshDewangan5
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
ajajkhan16
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptx
arjurakibulhasanrrr7
 

Similar to Pointers and arrays (20)

SP-First-Lecture.ppt
SP-First-Lecture.pptSP-First-Lecture.ppt
SP-First-Lecture.ppt
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
Unit3 C
Unit3 C Unit3 C
Unit3 C
 
Array
ArrayArray
Array
 
Array assignment
Array assignmentArray assignment
Array assignment
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
Engineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxEngineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptx
 
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointer
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptx
 

Recently uploaded

Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
Atif Razi
 
integral complex analysis chapter 06 .pdf
integral complex analysis chapter 06 .pdfintegral complex analysis chapter 06 .pdf
integral complex analysis chapter 06 .pdf
gaafergoudaay7aga
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
21UME003TUSHARDEB
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
Madan Karki
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Welding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdfWelding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdf
AjmalKhan50578
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
Divyanshu
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
gowrishankartb2005
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
cnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classicationcnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classication
SakkaravarthiShanmug
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
bjmsejournal
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 

Recently uploaded (20)

Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
 
integral complex analysis chapter 06 .pdf
integral complex analysis chapter 06 .pdfintegral complex analysis chapter 06 .pdf
integral complex analysis chapter 06 .pdf
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Welding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdfWelding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdf
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
cnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classicationcnn.pptx Convolutional neural network used for image classication
cnn.pptx Convolutional neural network used for image classication
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 

Pointers and arrays

  • 1. Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE Sri Eshwar College of Engineering Coimbatore
  • 2. No of similar data items grouped together to form a single entity is called as an Array. Syntax: datatype arrayName[subscript] * Subscript or number of elements in an array. Eg: int a,b,c; can be converted as int a[2]; (i.e) a[0], a[1], a[2] a[0] Initialization: int a[2] = {1,2,3}; index
  • 3. Memory Allocation: If a[0], a[1], a[2] represent the elements of an array what does array name represent? ArrayName holds the base address at which the array starts. 23456 23457 23458 23459 23460 23461 23462 ……….. a[0] a[1] a[2] Base address at which the array starts. Array Name holds this address. So a=23456 The data type is int, hence each element is allocated 2 bytes. ArrayName applied with index will retrieve the value of the array elements.
  • 4.  As the arrayName holds the base address of the array, it acts as a pointer.  ArrayName is a constant pointer. (i.e) it can always point to hold the same base address.  Bound checking will not be done for arrays in C. Garbage value or unknown value will be returned for the Array out of range retrieval. Eg: int a[10], b[10]; a and b holds the base address of their corresponding array. Below statements are trying to change the base address ‘a’ pointing to. Hence not allowed. a=b; // illegal. a++; // illegal. a = a+2; // illegal.
  • 5. Difference between pointer and an array. Pointer ArrayName Pointer value can be changed. (i.e) Address the pointer holds. Array Name is a constant pointer. Can be made to point to any value’s address. Points to same base address always. Sizeof (pointer) will be always same. (Mostly 2 bytes). Sizeof (arrayName) varies according to the datatype. Sizeof (pointer) will give the size of only the pointer. Sizeof (arrayName) gives the size of the whole array.
  • 6.  Pointer arithmetic is applicable to arrays also.  Just as pointer increment or decrement, arrayName also can be incremented or decremented. Only the control will be moved. The address to which the arrayName points to cannot be changed.  Pointer and arrayName are interchangeable.  Pointer –Pointer is applicable to arrays.  If ptr1 and ptr2 are the two pointer variables, then ptr1-ptr2= (ptr1 – ptr2) sizeof(data type)
  • 7. Example program where arrayName acts as a pointer.  No value is assigned for element a[2], so zero will be assigned for it. void main() { int i, a[3]={1,2}; clrscr(); printf("Base Address of array:%un", a); for(i=0;i<=2;i++) printf("Address of element %d:%u and value :%dn", i, (a+i), *(a+i)); getch(); } Output:
  • 8. Expression Equivalent Value displayed arrayName (Base Address +0) &(arrayName[0]) Base Address of the array arrayName + 1 (Base Address + (1 X sizeof(datatype)) &(arrayName[1]) Address of the second element of the array arrayName + 2 (Base Address + (2 X sizeof(datatype)) &(arrayName[2]) Address of the third element of the array Expression Equivalent Value displayed *arrayName (arrayName[0]) Value of the first element of the array *(arrayName + 1) (arrayName[1]) Value of the second element of the array *(arrayName + 2) (arrayName[2]) Value of the third element of the array
  • 9.  ArrayName holds the base address of the array, hence it can be assigned to a pointer and can be used for Array processing.  Auto increment or decrement can be done in the pointer variable.  Array name without the brackets is the pointer name and on the other end, a pointer can be indexed as if its an array. int a[10], *b; b=a // b holds the base address of array ‘a’. a[1] will give second element of the array. b[1] will also give 2nd element of the array. Here pointer ‘b’ acts as arrayName.
  • 10. Example program to process array using pointer(auto increment): void main() { int iCount; /* Loop Counter */ int iaAr[5]={1,2,3,4,5}; /* Array of 5 integers */ int *piArr=iaAr; /* Pointer to array */ for(iCount=0;iCount<5;iCount++) { /* Print the content of array using the pointer */ printf("n%d",*piArr); piArr++; // This auto increment cannot be done using arrayName } }
  • 11. Example program to process array using pointer: void main() { int iCount; /* Loop Counter */ int iaAr[5]={1,2,3,4,5}; /* Array of 5 integers */ int *piArr=iaAr; /* Pointer to array */ for(iCount=0;iCount<5;iCount++) { /* Print the content of array using the pointer */ printf("n %d",*(piArr+iCount)); } } Output:
  • 12. Example program to process array using pointer: void main() { int iCount; /* Loop Counter */ int iaAr[5]={1,2,3,4,5}; /* Array of 5 integers */ int *piArr=iaAr; /* Pointer to array */ for(iCount=0;iCount<5;iCount++) { /* Print the content of array using the pointer */ printf("n %d“, piArr[iCount]); // Indexing using pointer } } Output:
  • 13.  Indirection (or) dereference on a pointer will retrieve the value stored in that address.  A pointer can point to another pointer.  The concept will be same but the application of dereference operator will vary. As a result many layers of pointer can be formed and this called multiple indirection.  As such there is no limitation on the level of indirection but including great depth of indirection is difficult to follow and may lead to errors. Normally more than 2 level depth indirection is not required.  A pointer to a pointer has declaration is similar to that of a normal pointer but have more asterisk sign before them indicating the depth of the pointer. Declaration: int ** pointer; The above statement creates a pointer which points to pointer to a variable with int value.
  • 14. int i=10; 10 int *p = &i; 23456 int **ptr = &p; 34567 23456 23457 34567 34568 4456844567 ptr p i *ptr will retrieve the value stored in the address of ‘p’. It is also an address. To retrieve the value stored in the address ‘p’ is pointing to one more indirection is applied on *ptr. **ptr will retrieve the value 10. Expression Value ptr 34567 *ptr 23456 **ptr 10
  • 15. Example program for MultipleIndirection: void main () { int i = 10; int **p1; int *p2; clrscr(); p2 = &i; p1 = &p2; /* Multiple indirection */ printf (" **p1 = %d And *p2 = %d", **p1,*p2); getch(); } Output:
  • 16. Types of arrays: 1-Dimensional array (used in previous slides) 2-Dimensional array 3-Dimensional array and so on 1-D Array: Consists of many elements. Single row. Multiple columns. 2-D Array: Consists of many 1-D Arrays. Multiple rows and columns. 3-D Array: Consists of many 2-D Arrays.
  • 17. Memory Allocation for a 2-D array: Even though the 2-D array is seen as rows and columns, the memory will be allocated continuously. Declaration: Datatype arrayName[rows][columns]; Rows – No Of 1-D arrays . Columns – No Of elements in each 1-D Array. Eg: int a[10], b[10], c[10]; The above three arrays can be combined together to form a single array. This forms a 2-D array with rows and columns. int a[3][10]; Here a[0], a[1], a[2] are three 1-D arrays with 10 elements in each. Sizeof 2-D Array: No of elements in 2-D Array = Rows X Columns Size of 2-D Array = No of elements X sizeof(datatype)
  • 18. Initialization: int a[3][2]={1,2,3,4,5,6}; // Elements can be given continuously. int a[3][2]={ {1,2}, // Elements given for each 1- D Array. {3,4}, {5,6} }; int a[ ][2] = {1,2,3,4,5,6}; No Of Rows = No of Elements / No of columns 10000 10001 10004 10005 10008 10009 1 3 5 10002 10003 10006 10007 10010 10011 2 4 6 a[0] a[1] a[2] 2-D Array ‘a’
  • 19. Notes:  Like 1-D Array, 2-D arrayName is also a constant pointer.  In the previous example, 2-D arrayName ‘a’ is a constant pointer. It holds the base address of 2-D Array.  a[0], a[1], a[2] represent 1-D Arrays. Hence they hold the base address of each 1-D array correspondingly.  a[0][0], a[0][1], a[1][0], a[1][1], a[2][0], a[2][1] represent the individual elements. They hold the values.
  • 20. Example that illustrates the 1-d Array in each 2-D array: void main() { int iCount, iColCount; /* Loop Counter */ int iaAr[3][2]={1,2,3,4,5,6}; /* Array of 6 integers */ clrscr(); for( iCount=0;iCount<3;iCount++) { for( iColCount=0;iColCount<2;iColCount++) { printf("Address of element iaAr[%d][%d]: %un", iCount, iColCount, &(iaAr [iCount] [iColCount])); /* Address of each element*/ } } for( iCount=0; iCount<3; iCount++) { printf("Base Address of 1-D Array iaAr[%d]: %u n", iCount, iaAr[iCount]); } getch(); }
  • 22. Expression Equivalent Value displayed arrayName (or) *arrayName (arrayName[0]) Base Address of the 2-D Array (or) Base Address of the first 1-D Array arrayName + 1 (or) *(arrayName+1) (arrayName[1]) Base Address of the second 1-D Array arrayName + 2 (or) *(arrayName+2) (arrayName[2]) Base Address of the third 1-D Array Expression Equivalent Value displayed **arrayName (arrayName[0][0]) Value of the first element of the first 1-D array **(arrayName + 1) (arrayName[1][0]) Value of the first element of the second 1-D array **(arrayName + 2) (arrayName[2][0]) Value of the first element of the third 1-D array
  • 23. In simple way, arrayName[row][column] Each row (1-D Array) can be retrieved using, *(arrayName + rowNumber) Each element in each row can be retrieved using, *(*(arrayName + rowNumber)+ columnNumber) Expression Equivalent Value displayed *(*arrayName+0)+1) (arrayName[0][1]) Value of the second element of the first 1-D array *(*(arrayName + 1)+1) (arrayName[1][1]) Value of the second element of the second 1- D array *(*(arrayName + 2)+1) (arrayName[2][1]) Value of the second element of the third 1-D array
  • 24. Example to display 2-D Array using index: void main() { int iRowCount, iColCount; /* Loop Counter */ int iaAr[3][2]={1,2,3,4,5,6}; /* Array of 6 integers */ clrscr(); for( iRowCount=0;iRowCount<3;iRowCount++) { for(iColCount=0;iColCount<2;iColCount++) { printf("Value of element iaAr[%d][%d]: %dn", iRowCount, iColCount, iaAr[ iRowCount][iColCount]); } } getch(); }
  • 26. Example to display 2-D Array using arrayName as pointer: void main() { int iRowCount, iColCount; /* Loop Counter */ int iaAr[3][2]={1,2,3,4,5,6}; /* Array of 6 integers */ clrscr(); for(iRowCount=0;iRowCount<3;iRowCount++){ for(iColCount=0;iColCount<2;iColCount++){ printf("Value of element iaAr[%d][%d]: %dn", iRowCount, iColCount, *(*(iaAr+ iRowCount)+iColCount)); } } printf("n"); for(iRowCount=0;iRowCount<3;iRowCount++) { for(iColCount=0;iColCount<2;iColCount++) { printf("Value of element iaAr[%d][%d]: %dn", iRowCount, iColCount, *(iaAr[iRowCount]+iColCount)); } } getch(); }
  • 28. Notes:  In the same way, 2-D array is processed, 3-D, 4-D etc., arrays can also be processed.  3-D array is formed by combining multiple 2-D Arrays.  Indirection(*) first applied on the arrayName, will retrieve the 2-D array’s base address. Second indirection will retrieve the 1-D array’s base address. Third indirection will retrieve the element value.  Hence according to the dimension, the number of indirection operators to be applied to retrieve the value will vary. Eg: int a[2][3][4]; To retrieve the value of element a[0][1][3] using arrayName as pointer, *(*(*(a+0)+1)+3) has to be used.  No of Elements = 2X3X4 = 24  Sizeof(a) = 24X2 = 48 bytes