Lec 12. Multidimensional Arrays / Passing Arrays to Functions
1. Lecture 12Lecture 12
Version 1.0Version 1.0
Multidimensional ArraysMultidimensional Arrays
Passing Arrays to FuncionPassing Arrays to Funcion
2. 2Rushdi Shams, Dept of CSE, KUET, Bangladesh
Multidimensional ArrayMultidimensional Array
Arrays in C can have multiple subscriptsArrays in C can have multiple subscripts
A common use of multiple subscripted arrays isA common use of multiple subscripted arrays is
to represent tables of valuesto represent tables of values
To identify a particular table element, we mustTo identify a particular table element, we must
specify two subscripts: the first identifies thespecify two subscripts: the first identifies the
row and the second identifies the columnrow and the second identifies the column
3. 3Rushdi Shams, Dept of CSE, KUET, Bangladesh
Two Dimensional ArrayTwo Dimensional Array
Arrays that require two subscripts to identify aArrays that require two subscripts to identify a
particular element are called double subscriptedparticular element are called double subscripted
arraysarrays
ANSI standard supports at least 12 subscriptsANSI standard supports at least 12 subscripts
4. 4Rushdi Shams, Dept of CSE, KUET, Bangladesh
Two Dimensional ArrayTwo Dimensional Array
When we say a 3X4 array, we declare it as follows-When we say a 3X4 array, we declare it as follows-
int a [3][4];int a [3][4];
So, the first subscript denotes the row and the second subscriptSo, the first subscript denotes the row and the second subscript
denotes the columndenotes the column
5. 5Rushdi Shams, Dept of CSE, KUET, Bangladesh
Two Dimensional ArrayTwo Dimensional Array
A multiple subscripted array can be initialized inA multiple subscripted array can be initialized in
its declaration much like a single subscriptedits declaration much like a single subscripted
arrayarray
int b [2] [2] = {{1,2}, {3,4}};int b [2] [2] = {{1,2}, {3,4}};
in that case, 1 and 2 initialize b[0][0] and b[0][1]in that case, 1 and 2 initialize b[0][0] and b[0][1]
and 3 and 4 initialize b[1][0] and b[1][1]and 3 and 4 initialize b[1][0] and b[1][1]
6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh
Two Dimensional ArrayTwo Dimensional Array
10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh
Remember!!Remember!!
that while initializing a 2-D array it is necessarythat while initializing a 2-D array it is necessary
to mention the second (column) dimension,to mention the second (column) dimension,
whereas the first dimension (row) is optionalwhereas the first dimension (row) is optional
13. 13Rushdi Shams, Dept of CSE, KUET, Bangladesh
Three Dimensional ArraysThree Dimensional Arrays
14. 14Rushdi Shams, Dept of CSE, KUET, Bangladesh
Passing Arrays to FunctionsPassing Arrays to Functions
Array elements can be passed to a function byArray elements can be passed to a function by
calling the function by value, or by referencecalling the function by value, or by reference
In the call by value we pass values of arrayIn the call by value we pass values of array
elements to the functionelements to the function
in the call by reference we pass addresses ofin the call by reference we pass addresses of
array elements to the functionarray elements to the function
As we did not see call by reference yet, we willAs we did not see call by reference yet, we will
see that latersee that later
15. 15Rushdi Shams, Dept of CSE, KUET, Bangladesh
Passing Arrays to FunctionsPassing Arrays to Functions
17. 17Rushdi Shams, Dept of CSE, KUET, Bangladesh
Passing Arrays to FunctionsPassing Arrays to Functions
18. 18Rushdi Shams, Dept of CSE, KUET, Bangladesh
Function Prototype for functionsFunction Prototype for functions
taking arrays as argumentstaking arrays as arguments
The function prototype for this will be-The function prototype for this will be-
void print (int [ ][ ], int , int );void print (int [ ][ ], int , int );