SlideShare ist ein Scribd-Unternehmen logo
1 von 28
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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Smart Pointers in C++
Smart Pointers in C++Smart Pointers in C++
Smart Pointers in C++
 
C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
 
single linked list
single linked listsingle linked list
single linked list
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
Enums in c
Enums in cEnums in c
Enums in c
 
Strings
StringsStrings
Strings
 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
Pointers
PointersPointers
Pointers
 
Strings
StringsStrings
Strings
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
C programing -Structure
C programing -StructureC programing -Structure
C programing -Structure
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
13. Pointer and 2D array
13. Pointer  and  2D array13. Pointer  and  2D array
13. Pointer and 2D array
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 
This pointer
This pointerThis pointer
This pointer
 
C Structures And Unions
C  Structures And  UnionsC  Structures And  Unions
C Structures And Unions
 

Ähnlich wie Pointers and arrays

Ähnlich wie 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++
 
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
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 

Kürzlich hochgeladen

BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 

Kürzlich hochgeladen (20)

BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 

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