SlideShare a Scribd company logo
1 of 34
 In a two dimensional array, we can access each
element by using two subscripts.
first subscript represents the row number and
second subscript represents the column number
second subscript represents the column number
 Consider the following declaration statement,
 The above declaration statement creates a 3x4
matrix initialized with the following values:
 The element at ith row, jth column can be
accessed using
a[i][j].
 To access the value 7, we have to use
a[1][2].
a[1][2].
 Let us see how pointer is used to access the 2-D
array.
 As we know, name of the array gives the base
address of the array i.e., address of a[0][0].
 To access a[i][j] using base pointer of the array,
 To access a[i][j] using base pointer of the array,
we can use the following generalized form:
 The following figure shows how the above 2-D array
will be stored in memory.
 Array elements are stored in memory row by row.
 In a 2-D array, each row will be considered as a one
dimensional array.
 Hence 2-D array can be considered as a collection of several
one dimensional arrays.
 In this example, each row is an array of 4 integers.
 We know that the name of an array is a constant pointer that
points to 0th row which contains address 1000.
points to 0th row which contains address 1000.
 Consider a two dimensional array mat[ROWS] [COLS]
where ROWS and COLS are some integer value.
 We can use any one of the following two notations to
access the two dimensional array.
 The first int mat[][COLS] is general array notation.
 Whereas int (*mat)[COLS] is a pointer to array.
 For passing multidimensional arrays,
First array dimension does not have to be
specified.
The second dimension must be explicitly
The second dimension must be explicitly
specified or a pointer to pointer as a parameter
to receive the address.
 Returning a 2D array from function is not as straight as passing
an array to a function.
 Arrays in C are passed by reference, hence any changes made to
array passed as argument persists after the function.
 So you can accept the output array you need to return, as a
parameter to the function.
parameter to the function.
 The following program demonstrates how a 2-D array is returned
from a function by passing resultant array also as a parameter.
 In the following program, main() receives two matrices as input
and these two matrices along with resultant matrix are passed as
arguments to the function sum().
 The sum() function adds the two matrices and return the
resultant matrix as result to the main() function.
13. Pointer  and  2D array
13. Pointer  and  2D array
13. Pointer  and  2D array
13. Pointer  and  2D array
13. Pointer  and  2D array
13. Pointer  and  2D array

More Related Content

What's hot

Data Types - Premetive and Non Premetive
Data Types - Premetive and Non Premetive Data Types - Premetive and Non Premetive
Data Types - Premetive and Non Premetive Raj Naik
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member FunctionsMuhammad Hammad Waseem
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHISowmya Jyothi
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVAAnkita Totala
 
Application of Data structure
Application of Data structureApplication of Data structure
Application of Data structureDeepika051991
 
Array in c language
Array in c languageArray in c language
Array in c languagehome
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First SearchKevin Jadiya
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and StringTasnima Hamid
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in pythonhydpy
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
 

What's hot (20)

Data Types - Premetive and Non Premetive
Data Types - Premetive and Non Premetive Data Types - Premetive and Non Premetive
Data Types - Premetive and Non Premetive
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
Linked list
Linked listLinked list
Linked list
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Application of Data structure
Application of Data structureApplication of Data structure
Application of Data structure
 
Stack
StackStack
Stack
 
Array in c++
Array in c++Array in c++
Array in c++
 
Array in c language
Array in c languageArray in c language
Array in c language
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Strings
StringsStrings
Strings
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 

Similar to 13. Pointer and 2D array

Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional arrayRajendran
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to ArraysTareq Hasan
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysMaulen Bale
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfaroraopticals15
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functionsSwarup Boro
 
C UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDYC UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDYRajeshkumar Reddy
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in Carshpreetkaur07
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersC++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersANUSUYA S
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updatedvrgokila
 

Similar to 13. Pointer and 2D array (20)

Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional array
 
2D-Arrays
2D-Arrays2D-Arrays
2D-Arrays
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Pf presntation
Pf presntationPf presntation
Pf presntation
 
Arrays
ArraysArrays
Arrays
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
C UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDYC UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDY
 
2D Array
2D Array 2D Array
2D Array
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
Co&al lecture-08
Co&al lecture-08Co&al lecture-08
Co&al lecture-08
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersC++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about Pointers
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 

More from Gem WeBlog

Analysis of optimization algorithms
Analysis of optimization algorithmsAnalysis of optimization algorithms
Analysis of optimization algorithmsGem WeBlog
 
Particle swarm intelligence
Particle swarm intelligenceParticle swarm intelligence
Particle swarm intelligenceGem WeBlog
 
Nature inspired metaheuristics
Nature inspired metaheuristicsNature inspired metaheuristics
Nature inspired metaheuristicsGem WeBlog
 
Darwin's theory of evolution
Darwin's theory of evolutionDarwin's theory of evolution
Darwin's theory of evolutionGem WeBlog
 
no free-lunch theorem
no free-lunch theoremno free-lunch theorem
no free-lunch theoremGem WeBlog
 
Video and animation
Video and animationVideo and animation
Video and animationGem WeBlog
 
Mpeg video compression
Mpeg video compressionMpeg video compression
Mpeg video compressionGem WeBlog
 
Designing multimedia
Designing multimediaDesigning multimedia
Designing multimediaGem WeBlog
 
Testing and Rolling Out Enterprise Applications
Testing and Rolling Out Enterprise ApplicationsTesting and Rolling Out Enterprise Applications
Testing and Rolling Out Enterprise ApplicationsGem WeBlog
 
Constructing Enterprise Applications
Constructing Enterprise  ApplicationsConstructing Enterprise  Applications
Constructing Enterprise ApplicationsGem WeBlog
 
Architecting and Designing Enterprise Applications
Architecting and Designing Enterprise ApplicationsArchitecting and Designing Enterprise Applications
Architecting and Designing Enterprise ApplicationsGem WeBlog
 
Incepting Enterprise Applications
Incepting Enterprise ApplicationsIncepting Enterprise Applications
Incepting Enterprise ApplicationsGem WeBlog
 
Introduction to BEA
Introduction to BEAIntroduction to BEA
Introduction to BEAGem WeBlog
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and StructuresGem WeBlog
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationGem WeBlog
 
Function pointer
Function pointerFunction pointer
Function pointerGem WeBlog
 
10. NULL pointer
10. NULL pointer10. NULL pointer
10. NULL pointerGem WeBlog
 
12.string and pointer
12.string and pointer12.string and pointer
12.string and pointerGem WeBlog
 
7. Pointer Arithmetic
7. Pointer Arithmetic7. Pointer Arithmetic
7. Pointer ArithmeticGem WeBlog
 

More from Gem WeBlog (20)

Analysis of optimization algorithms
Analysis of optimization algorithmsAnalysis of optimization algorithms
Analysis of optimization algorithms
 
Particle swarm intelligence
Particle swarm intelligenceParticle swarm intelligence
Particle swarm intelligence
 
Nature inspired metaheuristics
Nature inspired metaheuristicsNature inspired metaheuristics
Nature inspired metaheuristics
 
Darwin's theory of evolution
Darwin's theory of evolutionDarwin's theory of evolution
Darwin's theory of evolution
 
no free-lunch theorem
no free-lunch theoremno free-lunch theorem
no free-lunch theorem
 
Video and animation
Video and animationVideo and animation
Video and animation
 
Mpeg video compression
Mpeg video compressionMpeg video compression
Mpeg video compression
 
Digital audio
Digital audioDigital audio
Digital audio
 
Designing multimedia
Designing multimediaDesigning multimedia
Designing multimedia
 
Testing and Rolling Out Enterprise Applications
Testing and Rolling Out Enterprise ApplicationsTesting and Rolling Out Enterprise Applications
Testing and Rolling Out Enterprise Applications
 
Constructing Enterprise Applications
Constructing Enterprise  ApplicationsConstructing Enterprise  Applications
Constructing Enterprise Applications
 
Architecting and Designing Enterprise Applications
Architecting and Designing Enterprise ApplicationsArchitecting and Designing Enterprise Applications
Architecting and Designing Enterprise Applications
 
Incepting Enterprise Applications
Incepting Enterprise ApplicationsIncepting Enterprise Applications
Incepting Enterprise Applications
 
Introduction to BEA
Introduction to BEAIntroduction to BEA
Introduction to BEA
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Function pointer
Function pointerFunction pointer
Function pointer
 
10. NULL pointer
10. NULL pointer10. NULL pointer
10. NULL pointer
 
12.string and pointer
12.string and pointer12.string and pointer
12.string and pointer
 
7. Pointer Arithmetic
7. Pointer Arithmetic7. Pointer Arithmetic
7. Pointer Arithmetic
 

Recently uploaded

COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Ramkumar k
 
Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257subhasishdas79
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 
8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessorAshwiniTodkar4
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdfKamal Acharya
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiessarkmank1
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...Amil baba
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...ppkakm
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxMustafa Ahmed
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...drmkjayanthikannan
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxpritamlangde
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARKOUSTAV SARKAR
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesRashidFaridChishti
 

Recently uploaded (20)

COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257Memory Interfacing of 8086 with DMA 8257
Memory Interfacing of 8086 with DMA 8257
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdf
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 

13. Pointer and 2D array

  • 1.
  • 2.  In a two dimensional array, we can access each element by using two subscripts. first subscript represents the row number and second subscript represents the column number second subscript represents the column number  Consider the following declaration statement,
  • 3.  The above declaration statement creates a 3x4 matrix initialized with the following values:
  • 4.  The element at ith row, jth column can be accessed using a[i][j].  To access the value 7, we have to use a[1][2]. a[1][2].
  • 5.  Let us see how pointer is used to access the 2-D array.  As we know, name of the array gives the base address of the array i.e., address of a[0][0].  To access a[i][j] using base pointer of the array,  To access a[i][j] using base pointer of the array, we can use the following generalized form:
  • 6.  The following figure shows how the above 2-D array will be stored in memory.  Array elements are stored in memory row by row.
  • 7.  In a 2-D array, each row will be considered as a one dimensional array.  Hence 2-D array can be considered as a collection of several one dimensional arrays.  In this example, each row is an array of 4 integers.  We know that the name of an array is a constant pointer that points to 0th row which contains address 1000. points to 0th row which contains address 1000.
  • 8.
  • 9.  Consider a two dimensional array mat[ROWS] [COLS] where ROWS and COLS are some integer value.  We can use any one of the following two notations to access the two dimensional array.  The first int mat[][COLS] is general array notation.  Whereas int (*mat)[COLS] is a pointer to array.
  • 10.
  • 11.  For passing multidimensional arrays, First array dimension does not have to be specified. The second dimension must be explicitly The second dimension must be explicitly specified or a pointer to pointer as a parameter to receive the address.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.  Returning a 2D array from function is not as straight as passing an array to a function.  Arrays in C are passed by reference, hence any changes made to array passed as argument persists after the function.  So you can accept the output array you need to return, as a parameter to the function. parameter to the function.  The following program demonstrates how a 2-D array is returned from a function by passing resultant array also as a parameter.  In the following program, main() receives two matrices as input and these two matrices along with resultant matrix are passed as arguments to the function sum().  The sum() function adds the two matrices and return the resultant matrix as result to the main() function.