SlideShare a Scribd company logo
1 of 16
Download to read offline
Multidimensional Arrays
2
Two-Dimensional Arrays
 Two-Dimensional Array Syntax
data_type variable_name[ number][ number ];
Array dimensions
Declarations of arrays Remarks
int a[100]; a one-demensional array
int b[2][7]; a two-demensional array
int c[5][3][2]; a three-demensional array
3
Two-Dimensional Arrays
 Logical placement of int a[3][4]
a[0][0] a[0][1] a[0][2] a[0][3]
a[1][0] a[1][1] a[1][2] a[1][3]
a[2][0] a[2][1] a[2][2] a[2][3]
row 0
row 1
col 0 col 1 col 2 col 3
row 2
4
Two-Dimensional Arrays
 Two-Demensional Arrays #include <stdio.h>
#define M 3 /* number of rows */
#define N 4 /* number of columns */
int main(void){
int a[M][N], i, j, sum = 0;
for ( i = 0; i < M; ++i )
for ( j = 0; j < N; ++j )
a[i][j] = i + j;
for ( i = 0; i < M; ++i ) {
for ( j = 0; j < N; ++j )
printf(“a[%d][%d] = %d “,
i, j, a[i][j] );
printf(“n”);
}
}
a[0][0] = 0 a[0][1] = 1 a[0][2] = 2 a[0][3] = 3
a[1][0] = 1 a[1][1] = 2 a[1][2] = 3 a[1][3] = 4
a[2][0] = 2 a[2][1] = 3 a[2][2] = 4 a[2][3] = 5
5
Two-Dimensional Arrays
 Physical Placement of int a[3][4]
– Two-Dimensional Array is actually stored
as the format of 1-Dimensional Array in the computer memory.
a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]
row 0 row 1 row 2
1000 1016 1032
6
Two-Dimensional Arrays
[Ex]
int a[2][3], *p ;
p = &a[0][0];
p + 0  &a[0][0]  a[0] + 0
p + 1  &a[0][1]  a[0] + 1
p + 2  &a[0][2]  a[0] + 2
p + 3  &a[1][0]  a[0] + 3  a[1] + 0
p + 4  &a[1][1]  a[0] + 4  a[1] + 1
p + 5  &a[1][2]  a[0] + 5  a[1] + 2
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
Two-Dimensional Arrays
 Physical placement of int a[3][4]
– If int a[3][4] begins at address 1000, What’s the next value?
7
a = ?
a + 1 = ?
a[0] = ?
a[0] + 1 = ?
a[1] = ?
a[1] + 1 = ?
a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]
a[0] a[1] a[2]
1000 1016 1032
8
Two-Dimensional Arrays
 Physical placement of int a[3][4]
– a is a constant, type is int (*)[4]
– a[0], a[1], a[2] are each constant, type is int*
a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]
a[0] a[1] a[2]
1000 1016 1032
&a[0][0] ==a[0]==(int*)1000 &a[1][0]==a[1]==(int*)1016 &a[2][0]==a[2]==(int*)1032
a==&a[0]=(int**)1000 (a+1)==&a[1]=(int**)1016 (a+2)==&a[2]=(int**)1032
9
Two-Dimensional Arrays
 Many ways to access the element of Two-
Dimensional array
– a[ i ] : a element in i th line of array a
– a[ i ][ j ] : a element in i th line and j th row of array a
– Array name a is equal to &a[0]
Expressions equivalent to a[ i ][ j ]
*( a[ i ] + j )
( *( a + i ) ) [ j ]
*( ( *( a + i ) ) + j )
*( &a[0][0] + 4 * i + j )
int a[3][4]
10
Two-Dimensional Arrays
#include <stdio.h>
void main() {
int a[3][4], j, k, sum = 0 ;
for( j = 0 ; j < 3 ; j++ )
for( k = 0 ; k < 4 ; k++ )
scanf( “%d”, &a[j][k] ) ;
for( j = 0 ; j < 3 ; j++ )
for( k = 0 ; k < 4 ; k++ )
sum += a[j][k] ;
printf( “%dn”, sum ) ;
}
 Passing Two-Dimensional Array to function
#include <stdio.h>
int sum(?????) { ... }
void main() {
int a[3][4], j, k, sum = 0 ;
for( j = 0 ; j < 3 ; j++ )
for( k = 0 ; k < 4 ; k++ )
scanf( “%d”, &a[j][k] ) ;
printf( “%dn”, sum(????) ) ;
}
11
Two-Dimensional Arrays
 Passing Two-Dimensional Array to function
int sum( int num[][4], int size )
{
for( j = 0 ; j < size ; j++ )
for( k = 0 ; k < 4 ; k++ )
sum += num[j][k] ;
}
printf( “%dn”, sum(a, 3) ) ;
int (*num)[4]
 Passing Two-Dimensional Array to function
– Why not this?
Two-Dimensional Arrays
12
int sum( int num[][], int size0, int size1 )
{
for( j = 0 ; j < size0 ; j++ )
for( k = 0 ; k < size1 ; k++ )
sum += num[j][k] ;
}
printf( “%dn”, sum(a, 3, 4) ) ;
If you wrote ‘ int num[3][4] ‘,
C compiler converts from num[i][j] to *(base_address + 4*i + j).
Thus, If don’t specify 2nd size(4)
Then can not convert from num[i][j] to *(base_address + 4*i + j).
Two-Dimensional Arrays
 Passing Two-Dimensional Array to function
– Type conversion from 2-Dimensional array to 1-Dimensional
array
13
int sum( int num[], int size0, int size1 )
{
for( j = 0 ; j < size0 ; j++ )
for( k = 0 ; k < size1 ; k++ )
sum += *(num+ size1*j + k) ;
}
printf( “%dn”, sum( (int*)a, 3, 4) ) ;
14
Multidimensional Arrays
 Passing Three-Dimensional Array to function
int sum( int num[][4][5], int size )
{
for( j = 0 ; j < size ; j++ )
for( k = 0 ; k < 4 ; k++ )
for( l = 0 ; l < 5 ; l++ )
sum += num[j][k][l] ;
}
printf( “%dn”, sum(a, 3) ) ;
int (*num)[4][5]
Multidimensional Arrays
 Passing Three-Dimensional Array to function
– Converting to the pointer of 1-dimensional array
15
int sum( int num[], int s0, int s1, int s2 )
{
for( j = 0 ; j < s0 ; j++ )
for( k = 0 ; k < s1 ; k++ )
for( l = 0 ; l < s2 ; l++ )
sum += *(num+ s1*s2*j + s2*k + l) ;
}
printf( “%dn”, sum((int*)a, 3, 4, 5) ) ;
16
Multidimensional Arrays
 Initialization of Multi-dimensional Array
[Ex]
int a[ 2 ][ 2 ][ 3 ] = { { {1,1,0}, {2,0,0} }, { {3,0,0}, {4,4,0} } };
[Ex]
int a[ 2 ][ 2 ][ 3 ] = { 0 }; All element are initialized with 0
[Ex]
int a[ ][ 2 ][ 3 ] = { { {1, 1}, {2} }, { {3}, {4, 4} } };
[Ex]
int a[ 2 ][ 2 ][ 3 ] = { 1, 1, 0, 2, 0, 0, 3, 0, 0, 4, 4, 0 } ;

More Related Content

What's hot

Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Kumar Boro
 

What's hot (20)

Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional array
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
 
Pf presntation
Pf presntationPf presntation
Pf presntation
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Multi-Dimensional Lists
Multi-Dimensional ListsMulti-Dimensional Lists
Multi-Dimensional Lists
 
Day 1c access, select ordering copy.pptx
Day 1c   access, select   ordering copy.pptxDay 1c   access, select   ordering copy.pptx
Day 1c access, select ordering copy.pptx
 
Numpy python cheat_sheet
Numpy python cheat_sheetNumpy python cheat_sheet
Numpy python cheat_sheet
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and Polynomial
 
Arrays in C
Arrays in CArrays in C
Arrays in C
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
 
C arrays
C arraysC arrays
C arrays
 
Array
ArrayArray
Array
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
Array
ArrayArray
Array
 
Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303
 

Similar to 11 1. multi-dimensional array eng

02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
Qundeel
 
Datastructure tree
Datastructure treeDatastructure tree
Datastructure tree
rantd
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1
Mook Prapasson
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
Nooryaseen9
 

Similar to 11 1. multi-dimensional array eng (20)

PPS Arrays Matrix operations
PPS Arrays Matrix operationsPPS Arrays Matrix operations
PPS Arrays Matrix operations
 
Java arrays
Java    arraysJava    arrays
Java arrays
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
 
unit-2-dsa.pptx
unit-2-dsa.pptxunit-2-dsa.pptx
unit-2-dsa.pptx
 
SP-First-Lecture.ppt
SP-First-Lecture.pptSP-First-Lecture.ppt
SP-First-Lecture.ppt
 
7. arrays
7. arrays7. arrays
7. arrays
 
Vcs16
Vcs16Vcs16
Vcs16
 
Chandan
ChandanChandan
Chandan
 
C programs
C programsC programs
C programs
 
Arrays in CPP
Arrays in CPPArrays in CPP
Arrays in CPP
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
 
Datastructure tree
Datastructure treeDatastructure tree
Datastructure tree
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2
 
Gentle Introduction to Functional Programming
Gentle Introduction to Functional ProgrammingGentle Introduction to Functional Programming
Gentle Introduction to Functional Programming
 

More from 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
웅식 전
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
웅식 전
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
웅식 전
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
웅식 전
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
웅식 전
 

More from 웅식 전 (20)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
13. structure
13. structure13. structure
13. structure
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
9. pointer
9. pointer9. pointer
9. pointer
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
 
6. function
6. function6. function
6. function
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
 
4. loop
4. loop4. loop
4. loop
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

11 1. multi-dimensional array eng

  • 2. 2 Two-Dimensional Arrays  Two-Dimensional Array Syntax data_type variable_name[ number][ number ]; Array dimensions Declarations of arrays Remarks int a[100]; a one-demensional array int b[2][7]; a two-demensional array int c[5][3][2]; a three-demensional array
  • 3. 3 Two-Dimensional Arrays  Logical placement of int a[3][4] a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] row 0 row 1 col 0 col 1 col 2 col 3 row 2
  • 4. 4 Two-Dimensional Arrays  Two-Demensional Arrays #include <stdio.h> #define M 3 /* number of rows */ #define N 4 /* number of columns */ int main(void){ int a[M][N], i, j, sum = 0; for ( i = 0; i < M; ++i ) for ( j = 0; j < N; ++j ) a[i][j] = i + j; for ( i = 0; i < M; ++i ) { for ( j = 0; j < N; ++j ) printf(“a[%d][%d] = %d “, i, j, a[i][j] ); printf(“n”); } } a[0][0] = 0 a[0][1] = 1 a[0][2] = 2 a[0][3] = 3 a[1][0] = 1 a[1][1] = 2 a[1][2] = 3 a[1][3] = 4 a[2][0] = 2 a[2][1] = 3 a[2][2] = 4 a[2][3] = 5
  • 5. 5 Two-Dimensional Arrays  Physical Placement of int a[3][4] – Two-Dimensional Array is actually stored as the format of 1-Dimensional Array in the computer memory. a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] row 0 row 1 row 2 1000 1016 1032
  • 6. 6 Two-Dimensional Arrays [Ex] int a[2][3], *p ; p = &a[0][0]; p + 0  &a[0][0]  a[0] + 0 p + 1  &a[0][1]  a[0] + 1 p + 2  &a[0][2]  a[0] + 2 p + 3  &a[1][0]  a[0] + 3  a[1] + 0 p + 4  &a[1][1]  a[0] + 4  a[1] + 1 p + 5  &a[1][2]  a[0] + 5  a[1] + 2 a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
  • 7. Two-Dimensional Arrays  Physical placement of int a[3][4] – If int a[3][4] begins at address 1000, What’s the next value? 7 a = ? a + 1 = ? a[0] = ? a[0] + 1 = ? a[1] = ? a[1] + 1 = ? a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] a[0] a[1] a[2] 1000 1016 1032
  • 8. 8 Two-Dimensional Arrays  Physical placement of int a[3][4] – a is a constant, type is int (*)[4] – a[0], a[1], a[2] are each constant, type is int* a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] a[0] a[1] a[2] 1000 1016 1032 &a[0][0] ==a[0]==(int*)1000 &a[1][0]==a[1]==(int*)1016 &a[2][0]==a[2]==(int*)1032 a==&a[0]=(int**)1000 (a+1)==&a[1]=(int**)1016 (a+2)==&a[2]=(int**)1032
  • 9. 9 Two-Dimensional Arrays  Many ways to access the element of Two- Dimensional array – a[ i ] : a element in i th line of array a – a[ i ][ j ] : a element in i th line and j th row of array a – Array name a is equal to &a[0] Expressions equivalent to a[ i ][ j ] *( a[ i ] + j ) ( *( a + i ) ) [ j ] *( ( *( a + i ) ) + j ) *( &a[0][0] + 4 * i + j ) int a[3][4]
  • 10. 10 Two-Dimensional Arrays #include <stdio.h> void main() { int a[3][4], j, k, sum = 0 ; for( j = 0 ; j < 3 ; j++ ) for( k = 0 ; k < 4 ; k++ ) scanf( “%d”, &a[j][k] ) ; for( j = 0 ; j < 3 ; j++ ) for( k = 0 ; k < 4 ; k++ ) sum += a[j][k] ; printf( “%dn”, sum ) ; }  Passing Two-Dimensional Array to function #include <stdio.h> int sum(?????) { ... } void main() { int a[3][4], j, k, sum = 0 ; for( j = 0 ; j < 3 ; j++ ) for( k = 0 ; k < 4 ; k++ ) scanf( “%d”, &a[j][k] ) ; printf( “%dn”, sum(????) ) ; }
  • 11. 11 Two-Dimensional Arrays  Passing Two-Dimensional Array to function int sum( int num[][4], int size ) { for( j = 0 ; j < size ; j++ ) for( k = 0 ; k < 4 ; k++ ) sum += num[j][k] ; } printf( “%dn”, sum(a, 3) ) ; int (*num)[4]
  • 12.  Passing Two-Dimensional Array to function – Why not this? Two-Dimensional Arrays 12 int sum( int num[][], int size0, int size1 ) { for( j = 0 ; j < size0 ; j++ ) for( k = 0 ; k < size1 ; k++ ) sum += num[j][k] ; } printf( “%dn”, sum(a, 3, 4) ) ; If you wrote ‘ int num[3][4] ‘, C compiler converts from num[i][j] to *(base_address + 4*i + j). Thus, If don’t specify 2nd size(4) Then can not convert from num[i][j] to *(base_address + 4*i + j).
  • 13. Two-Dimensional Arrays  Passing Two-Dimensional Array to function – Type conversion from 2-Dimensional array to 1-Dimensional array 13 int sum( int num[], int size0, int size1 ) { for( j = 0 ; j < size0 ; j++ ) for( k = 0 ; k < size1 ; k++ ) sum += *(num+ size1*j + k) ; } printf( “%dn”, sum( (int*)a, 3, 4) ) ;
  • 14. 14 Multidimensional Arrays  Passing Three-Dimensional Array to function int sum( int num[][4][5], int size ) { for( j = 0 ; j < size ; j++ ) for( k = 0 ; k < 4 ; k++ ) for( l = 0 ; l < 5 ; l++ ) sum += num[j][k][l] ; } printf( “%dn”, sum(a, 3) ) ; int (*num)[4][5]
  • 15. Multidimensional Arrays  Passing Three-Dimensional Array to function – Converting to the pointer of 1-dimensional array 15 int sum( int num[], int s0, int s1, int s2 ) { for( j = 0 ; j < s0 ; j++ ) for( k = 0 ; k < s1 ; k++ ) for( l = 0 ; l < s2 ; l++ ) sum += *(num+ s1*s2*j + s2*k + l) ; } printf( “%dn”, sum((int*)a, 3, 4, 5) ) ;
  • 16. 16 Multidimensional Arrays  Initialization of Multi-dimensional Array [Ex] int a[ 2 ][ 2 ][ 3 ] = { { {1,1,0}, {2,0,0} }, { {3,0,0}, {4,4,0} } }; [Ex] int a[ 2 ][ 2 ][ 3 ] = { 0 }; All element are initialized with 0 [Ex] int a[ ][ 2 ][ 3 ] = { { {1, 1}, {2} }, { {3}, {4, 4} } }; [Ex] int a[ 2 ][ 2 ][ 3 ] = { 1, 1, 0, 2, 0, 0, 3, 0, 0, 4, 4, 0 } ;