SlideShare a Scribd company logo
For more Https://www.ThesisScientist.com
Unit 6
Arrays
Introduction to Arrays
An array is a group of data items of same data type that share a common name. Ordinary variables are
capable of holding only one value at a time. If we want to store more than one value at a time in a single
variable, we use arrays.
An array is a collective name given to a group of similar variables. Each member in the group is referred to
by its position in the group.
Arrays are alloted the memory in a strictly contiguous fashion. The simplest array is one dimensional array
which is a list of variables of same data type. An array of one dimensional arrays is called a two
dimensional array.
One Dimensional Array
A list of items can be given one variable name using only one subscript and such a variable is called a one
dimensional array.
e.g.: If we want to store a set of five numbers by an array variable number. Then it will be
accomplished in the following way:
int number [5];
This declaration will reserve five contiguous memory locations as shown below:
Number [0] Number [1] Number [2] Number [3] Number [4]
As C performs no bounds checking, care should be taken to ensure that the array indices are within the
declared limits. Also, indexing in C begins from 0 and not from 1.
Array Declaration
Arrays are defined in the same manner as ordinary variables, except that each array name must be
accompanied by the size specification.
The general form of array declaration is:
data-type array-name [size];
data-type specifies the type of array, size is a positive integer number or symbolic constant that indicates
the maximum number of elements that can be stored in the array.
e.g.: float height [50];
This declaration declares an array named height containing 50 elements of type float.
NOTE The compiler will interpret first element as height [0]. As in C, the array elements are
induced for 0 to [size-1].
Array Initialization
The elements of an array can be initialized in the same way as the ordinary variables, when they are
declared. Given below are some examples which show how the arrays are initialized.
static int num [6] = {2, 4, 5, 45, 12};
static int n [ ] = {2, 4, 5, 45, 12};
static float press [ ] = {12.5, 32.4, -23.7, -11.3};
In these examples note the following points:
(a) Till the array elements are not given any specific values, they contain garbage value.
(b) If the array is initialized where it is declared, its storage class must be either static or extern. If the
storage class is static, all the elements are initialized by 0.
(c) If the array is initialized where it is declared, mentioning the dimension of the array is optional.
Accessing Elements of an Array
Once an array is declared, individual elements of the array are referred using subscript or index number.
This number specifies the element's position in the array. All the elements of the array are numbered
starting from 0. Thus number [5] is actually the sixth element of an array.
Entering Data into an Array
It can be explained by the following examples:
main( )
{
int num [6];
int count;
for (count = 0; count < 6; count ++)
{
printf ("n Enter %d element:" count+1);
scanf ("%d", &num [count]);
}
}
In this example, using the for loop, the process of asking and receiving the marks is accomplished. When
count has the value zero, the scanf( ) statement will cause the value to be stored at num [0]. This process
continues until count has the value greater than 5.
Reading Data from an Array
Consider the program given above. It has entered 6 values in the array num. Now to read values from this
array, we will again use for Loop to access each cell. The given program segment explains the retrieval of
the values from the array.
for (count = 0; count < 6; count ++)
{
printf ("n %d value =", num [count]);
}
Memory Representation of Array
Consider the following array declaration:
int arr[8];
16 bytes get immediately reserved in memory because each of the 8 integers would be 2 bytes long and
since the array is not being initialized, all eight values present in it would be garbage values.
Whatever be the initial values, all the array elements would always be present in contiguous memory
location. This arrangement of array elements in memory is shown below.
12 34 66 -45 23 346 77 98
4002 4004 4006 4008 4010 4012 4014 4016
Value
Address
1212 34 66 -45 23 346 77 98
4002 4004 4006 4008 4010 4012 4014 4016
Value
Address
In C, there is no check to see if the subscript used for an array exceeds the size of the array. Data entered
with a subscript exceeding the array size will simply be placed in memory outside the array. This will
lead to unpredictable results and there will be no error message to warn you that you are going beyond
the array size. So to see to it that you do not reach beyond the array size is entirely the programmer's
botheration and not the compiler's.
Strings
Just as a group of integers can be stored in an integer array, group of characters can be stored in a character
array or "strings". The string constant is a one dimensional array of characters terminated by null character
('0'). This null character '0' (ASCII value 0) is different from 'O' (ASCII value 48).
The terminating null character is important because it is the only way the function that works with string
can know where the string ends.
e.g.: Static char name [ ] = {'K', 'R', 'I', 'S', 'H', '0'};
This example shows the declaration and initialization of a character array. The array elements of a character
array are stored in contiguous locations with each element occupying one byte of memory.
K R I S H N A ‘0’
4001 4002 4003 4004 4005 4006 4007 4009
NOTEOTE1. Contrary to the numeric array where a 5 digit number can be stored in one array cell, in
the character arrays only a single character can be stored in one cell. So in order to
store an array of strings, a 2-dimensional array is required.
2. As scanf( ) function is not capable of receiving multi word string, such strings should be
Two Dimensional Array
This is a table of four rows and three columns. Such a table of items can be defined using two dimensional
arrays.
General form of declaring a 2-D array is
data_type array_name [row_size] [colum_size];
Initialization of a 2-Dimensional Array
Two dimensional arrays may be initialized by a list of initial values enclosed in braces following their
declaration.
e.g.: static int table [2] [3] = {0, 0, 0, 1, 1, 1};
initializes the elements of the first row to 0 and the second row to one. The initialization is done by row.
The aforesaid statement can be equivalently written as
static int table [2] [3] = {{0, 0, 0}, {1, 1, 1}};
by surrounding the elements of each row by braces.
We can also initialize a two dimensional array in the form of a matrix as shown below:
static int table [2] [3] = {{0, 0, 0},
{1, 1, 1}};
The syntax of the above statement. Commas are required after each brace that closes off a row,
except in the case of the last row.
If the values are missing in an initializer, they are automatically set to 0. For instance, the statement
static int table [2] [3] = {{1, 1},
{2}};
will initialize the first two elements of the first row to one, the first element of the second row to two, and
all the other elements to 0.
When all the elements are to be initialized to 0, the following short cut method may be used.
static int m [3] [5] = {{0}, {0}, {0}};
The first element of each row is explicitly initialized to 0 while other elements are automatically initialized
to 0.
While initializing an array, it is necessary to mention the second (column) dimension, whereas the first
dimension (row) is optional. Thus, the following declarations are acceptable.
static int arr [2] [3] = {12, 34, 23, 45, 56, 45};
static int arr [ ] [3] = {12, 34, 23, 45, 56, 45 };
Memory Representation of Two Dimensional Array
In memory, whether it is a one dimensional or a two dimensional array, the array elements are stored in one
continuous chain.
The arrangement of array elements of a two dimensional array of students, which contains roll numbers in
one column and the marks in the other (in memory) is shown below:
1234
5002 5004 5006 5008 5010 5012 5014 5016
Value
Address
1234 1234 1234 1234 1234 1234 1234
S[0][0] S[0][1] S[1][0] S[1][1] S[2][0] S[2][1] S[3][0] S[3][0]Notation
1234
5002 5004 5006 5008 5010 5012 5014 5016
Value
Address
1234 1234 1234 1234 1234 1234 1234
S[0][0] S[0][1] S[1][0] S[1][1] S[2][0] S[2][1] S[3][0] S[3][0]Notation
e.g.: 1. Program that stores roll number and marks obtained by a student side by
side in a matrix
main( )
{
int stud [4] [2];
int i, j;
for (i = 0; i < = 3; i++)
{
printf ("n Enter roll no. and marks");
scanf ("%d%d", &stud [i] [0], &stud[i] [1]);
}
for (i = 0; i < = 3; i++)
printf ("%d%dn", stud [i] [0], stud [i] [0];
}
There are two parts to the program, in the first part through a for Loop
we read in the values of roll number and marks, whereas in second part
through another for Loop we print out these values.
Multi-dimensional Array
C allows arrays of three or more dimensions. Multi-dimensional arrays are defined in much the same
manner as one-dimensional arrays, except that a separate pair of square brackets is required for each
subscript.
The general form of a multi-dimensional array is
data_type array_name [s1] [s2] [s3] . . . [sm];
e.g.: int survey [3] [5] [12];
float table [5] [4] [5] [3];
Here, survey is a 3-dimensional array declared to contain 180 integer_type elements. Similarly, table is a 4-
dimensional array containing 300 elements of floating point type.
An example of initializing a 4-dimensional array:
static int arr [3] [4] [2] = {{{2, 4}, {7, 8}, {3, 4}, {5, 6},},
{{7, 6}, {3, 4}, {5, 3}, {2, 3}, },
{{8, 9}, {7, 2}, {3, 4}, {6, 1}, }
};
In this example, the outer array has three elements, each of which is a two dimensional array of four rows,
each of which is a one dimensional array of two elements.

More Related Content

PDF
Array
hjasjhd
 
PPTX
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
PPTX
concept of Array, 1D & 2D array
Sangani Ankur
 
PPT
Array in c
Ravi Gelani
 
PPT
Chap09
Terry Yoast
 
PPT
Algo>Arrays
Ain-ul-Moiz Khawaja
 
PPTX
arrays in c
vidhi mehta
 
PPT
Arrays in c
vampugani
 
Array
hjasjhd
 
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
concept of Array, 1D & 2D array
Sangani Ankur
 
Array in c
Ravi Gelani
 
Chap09
Terry Yoast
 
Algo>Arrays
Ain-ul-Moiz Khawaja
 
arrays in c
vidhi mehta
 
Arrays in c
vampugani
 

What's hot (20)

PPTX
C++ programming (Array)
طارق بالحارث
 
PPT
One dimensional 2
Rajendran
 
PPTX
Array in c++
Mahesha Mano
 
PPT
C programming , array 2020
Osama Ghandour Geris
 
PPTX
Arrays in c
Jeeva Nanthini
 
PPTX
Arrays in c
CHANDAN KUMAR
 
PDF
Arrays-Computer programming
nmahi96
 
PPTX
Array
HarshKumar943076
 
PPTX
Multi-Dimensional Lists
primeteacher32
 
DOC
Arrays and Strings
Dr.Subha Krishna
 
PPTX
Array and string
prashant chelani
 
PPTX
ARRAY
ayush raj
 
PPTX
Array
Anil Neupane
 
PPTX
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
PPTX
Data structure array
MajidHamidAli
 
PPTX
Introduction to Array ppt
sandhya yadav
 
PPT
Array
PRN USM
 
PPTX
Programming in c Arrays
janani thirupathi
 
PPT
C++ Arrays
أحمد محمد
 
PPT
Data Structure and Algorithms Arrays
ManishPrajapati78
 
C++ programming (Array)
طارق بالحارث
 
One dimensional 2
Rajendran
 
Array in c++
Mahesha Mano
 
C programming , array 2020
Osama Ghandour Geris
 
Arrays in c
Jeeva Nanthini
 
Arrays in c
CHANDAN KUMAR
 
Arrays-Computer programming
nmahi96
 
Multi-Dimensional Lists
primeteacher32
 
Arrays and Strings
Dr.Subha Krishna
 
Array and string
prashant chelani
 
ARRAY
ayush raj
 
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
Data structure array
MajidHamidAli
 
Introduction to Array ppt
sandhya yadav
 
Array
PRN USM
 
Programming in c Arrays
janani thirupathi
 
C++ Arrays
أحمد محمد
 
Data Structure and Algorithms Arrays
ManishPrajapati78
 
Ad

Similar to Introduction to Arrays in C (20)

PDF
ARRAYS
muniryaseen
 
PDF
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
PDF
Arrays and library functions
Swarup Boro
 
PDF
Unit ii data structure-converted
Shri Shankaracharya College, Bhilai,Junwani
 
PPTX
Module 4- Arrays and Strings
nikshaikh786
 
PPTX
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
PDF
Array&amp;string
chanchal ghosh
 
PDF
02 arrays
Rajan Gautam
 
PPTX
3.ArraysandPointers.pptx
FolkAdonis
 
PDF
Array and its types and it's implemented programming Final.pdf
ajajkhan16
 
PPTX
Arrays
Trupti Agrawal
 
PDF
Arrays
Steven Wallach
 
PPTX
C Programming Unit-3
Vikram Nandini
 
PPT
2 arrays
trixiacruz
 
PPTX
Array in C
adityas29
 
ARRAYS
muniryaseen
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
Arrays and library functions
Swarup Boro
 
Unit ii data structure-converted
Shri Shankaracharya College, Bhilai,Junwani
 
Module 4- Arrays and Strings
nikshaikh786
 
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
Array&amp;string
chanchal ghosh
 
02 arrays
Rajan Gautam
 
3.ArraysandPointers.pptx
FolkAdonis
 
Array and its types and it's implemented programming Final.pdf
ajajkhan16
 
C Programming Unit-3
Vikram Nandini
 
2 arrays
trixiacruz
 
Array in C
adityas29
 
Ad

More from Thesis Scientist Private Limited (20)

PDF
HTML guide for beginners
Thesis Scientist Private Limited
 
PDF
Ransomware attacks 2017
Thesis Scientist Private Limited
 
PDF
How to write a Great Research Paper?
Thesis Scientist Private Limited
 
PDF
Research Process design
Thesis Scientist Private Limited
 
PDF
How to write a good Dissertation/ Thesis
Thesis Scientist Private Limited
 
PDF
How to write a Research Paper
Thesis Scientist Private Limited
 
PDF
Internet security tips for Businesses
Thesis Scientist Private Limited
 
PDF
How to deal with a Compulsive liar
Thesis Scientist Private Limited
 
PDF
Driverless car Google
Thesis Scientist Private Limited
 
PDF
Podcast tips beginners
Thesis Scientist Private Limited
 
PDF
Vastu for Career Success
Thesis Scientist Private Limited
 
PDF
Reliance jio broadband
Thesis Scientist Private Limited
 
PDF
Job Satisfaction definition
Thesis Scientist Private Limited
 
PDF
Mistakes in Advertising
Thesis Scientist Private Limited
 
PDF
Contributor in a sentence
Thesis Scientist Private Limited
 
PDF
Different Routing protocols
Thesis Scientist Private Limited
 
PDF
Ad hoc network routing protocols
Thesis Scientist Private Limited
 
PDF
Latest Thesis Topics for Fog computing
Thesis Scientist Private Limited
 
PDF
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
Thesis Scientist Private Limited
 
HTML guide for beginners
Thesis Scientist Private Limited
 
Ransomware attacks 2017
Thesis Scientist Private Limited
 
How to write a Great Research Paper?
Thesis Scientist Private Limited
 
Research Process design
Thesis Scientist Private Limited
 
How to write a good Dissertation/ Thesis
Thesis Scientist Private Limited
 
How to write a Research Paper
Thesis Scientist Private Limited
 
Internet security tips for Businesses
Thesis Scientist Private Limited
 
How to deal with a Compulsive liar
Thesis Scientist Private Limited
 
Driverless car Google
Thesis Scientist Private Limited
 
Podcast tips beginners
Thesis Scientist Private Limited
 
Vastu for Career Success
Thesis Scientist Private Limited
 
Reliance jio broadband
Thesis Scientist Private Limited
 
Job Satisfaction definition
Thesis Scientist Private Limited
 
Mistakes in Advertising
Thesis Scientist Private Limited
 
Contributor in a sentence
Thesis Scientist Private Limited
 
Different Routing protocols
Thesis Scientist Private Limited
 
Ad hoc network routing protocols
Thesis Scientist Private Limited
 
Latest Thesis Topics for Fog computing
Thesis Scientist Private Limited
 
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
Thesis Scientist Private Limited
 

Recently uploaded (20)

PPTX
AgentX UiPath Community Webinar series - Delhi
RohitRadhakrishnan8
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PDF
Principles of Food Science and Nutritions
Dr. Yogesh Kumar Kosariya
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PDF
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
demidovs1
 
PPTX
EE3303-EM-I 25.7.25 electrical machines.pptx
Nagen87
 
PDF
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
PDF
Introduction to Data Science: data science process
ShivarkarSandip
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PPTX
TE-AI-Unit VI notes using planning model
swatigaikwad6389
 
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Dr. Rahul Kumar
 
PDF
B.Tech Data Science Program (Industry Integrated ) Syllabus
rvray078
 
PPTX
Azure-DevOps-Training presentation downloadable
NamanGoyal428595
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PDF
Queuing formulas to evaluate throughputs and servers
gptshubham
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
AgentX UiPath Community Webinar series - Delhi
RohitRadhakrishnan8
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
Principles of Food Science and Nutritions
Dr. Yogesh Kumar Kosariya
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
demidovs1
 
EE3303-EM-I 25.7.25 electrical machines.pptx
Nagen87
 
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
Introduction to Data Science: data science process
ShivarkarSandip
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
TE-AI-Unit VI notes using planning model
swatigaikwad6389
 
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Dr. Rahul Kumar
 
B.Tech Data Science Program (Industry Integrated ) Syllabus
rvray078
 
Azure-DevOps-Training presentation downloadable
NamanGoyal428595
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
Queuing formulas to evaluate throughputs and servers
gptshubham
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 

Introduction to Arrays in C

  • 1. For more Https://www.ThesisScientist.com Unit 6 Arrays Introduction to Arrays An array is a group of data items of same data type that share a common name. Ordinary variables are capable of holding only one value at a time. If we want to store more than one value at a time in a single variable, we use arrays. An array is a collective name given to a group of similar variables. Each member in the group is referred to by its position in the group. Arrays are alloted the memory in a strictly contiguous fashion. The simplest array is one dimensional array which is a list of variables of same data type. An array of one dimensional arrays is called a two dimensional array. One Dimensional Array A list of items can be given one variable name using only one subscript and such a variable is called a one dimensional array. e.g.: If we want to store a set of five numbers by an array variable number. Then it will be accomplished in the following way: int number [5]; This declaration will reserve five contiguous memory locations as shown below: Number [0] Number [1] Number [2] Number [3] Number [4] As C performs no bounds checking, care should be taken to ensure that the array indices are within the declared limits. Also, indexing in C begins from 0 and not from 1. Array Declaration Arrays are defined in the same manner as ordinary variables, except that each array name must be accompanied by the size specification. The general form of array declaration is: data-type array-name [size];
  • 2. data-type specifies the type of array, size is a positive integer number or symbolic constant that indicates the maximum number of elements that can be stored in the array. e.g.: float height [50]; This declaration declares an array named height containing 50 elements of type float. NOTE The compiler will interpret first element as height [0]. As in C, the array elements are induced for 0 to [size-1]. Array Initialization The elements of an array can be initialized in the same way as the ordinary variables, when they are declared. Given below are some examples which show how the arrays are initialized. static int num [6] = {2, 4, 5, 45, 12}; static int n [ ] = {2, 4, 5, 45, 12}; static float press [ ] = {12.5, 32.4, -23.7, -11.3}; In these examples note the following points: (a) Till the array elements are not given any specific values, they contain garbage value. (b) If the array is initialized where it is declared, its storage class must be either static or extern. If the storage class is static, all the elements are initialized by 0. (c) If the array is initialized where it is declared, mentioning the dimension of the array is optional. Accessing Elements of an Array Once an array is declared, individual elements of the array are referred using subscript or index number. This number specifies the element's position in the array. All the elements of the array are numbered starting from 0. Thus number [5] is actually the sixth element of an array. Entering Data into an Array It can be explained by the following examples: main( ) { int num [6]; int count; for (count = 0; count < 6; count ++) { printf ("n Enter %d element:" count+1); scanf ("%d", &num [count]); } } In this example, using the for loop, the process of asking and receiving the marks is accomplished. When count has the value zero, the scanf( ) statement will cause the value to be stored at num [0]. This process continues until count has the value greater than 5. Reading Data from an Array Consider the program given above. It has entered 6 values in the array num. Now to read values from this array, we will again use for Loop to access each cell. The given program segment explains the retrieval of the values from the array.
  • 3. for (count = 0; count < 6; count ++) { printf ("n %d value =", num [count]); } Memory Representation of Array Consider the following array declaration: int arr[8]; 16 bytes get immediately reserved in memory because each of the 8 integers would be 2 bytes long and since the array is not being initialized, all eight values present in it would be garbage values. Whatever be the initial values, all the array elements would always be present in contiguous memory location. This arrangement of array elements in memory is shown below. 12 34 66 -45 23 346 77 98 4002 4004 4006 4008 4010 4012 4014 4016 Value Address 1212 34 66 -45 23 346 77 98 4002 4004 4006 4008 4010 4012 4014 4016 Value Address In C, there is no check to see if the subscript used for an array exceeds the size of the array. Data entered with a subscript exceeding the array size will simply be placed in memory outside the array. This will lead to unpredictable results and there will be no error message to warn you that you are going beyond the array size. So to see to it that you do not reach beyond the array size is entirely the programmer's botheration and not the compiler's. Strings Just as a group of integers can be stored in an integer array, group of characters can be stored in a character array or "strings". The string constant is a one dimensional array of characters terminated by null character ('0'). This null character '0' (ASCII value 0) is different from 'O' (ASCII value 48). The terminating null character is important because it is the only way the function that works with string can know where the string ends. e.g.: Static char name [ ] = {'K', 'R', 'I', 'S', 'H', '0'}; This example shows the declaration and initialization of a character array. The array elements of a character array are stored in contiguous locations with each element occupying one byte of memory. K R I S H N A ‘0’ 4001 4002 4003 4004 4005 4006 4007 4009 NOTEOTE1. Contrary to the numeric array where a 5 digit number can be stored in one array cell, in the character arrays only a single character can be stored in one cell. So in order to store an array of strings, a 2-dimensional array is required. 2. As scanf( ) function is not capable of receiving multi word string, such strings should be Two Dimensional Array This is a table of four rows and three columns. Such a table of items can be defined using two dimensional arrays.
  • 4. General form of declaring a 2-D array is data_type array_name [row_size] [colum_size]; Initialization of a 2-Dimensional Array Two dimensional arrays may be initialized by a list of initial values enclosed in braces following their declaration. e.g.: static int table [2] [3] = {0, 0, 0, 1, 1, 1}; initializes the elements of the first row to 0 and the second row to one. The initialization is done by row. The aforesaid statement can be equivalently written as static int table [2] [3] = {{0, 0, 0}, {1, 1, 1}}; by surrounding the elements of each row by braces. We can also initialize a two dimensional array in the form of a matrix as shown below: static int table [2] [3] = {{0, 0, 0}, {1, 1, 1}}; The syntax of the above statement. Commas are required after each brace that closes off a row, except in the case of the last row. If the values are missing in an initializer, they are automatically set to 0. For instance, the statement static int table [2] [3] = {{1, 1}, {2}}; will initialize the first two elements of the first row to one, the first element of the second row to two, and all the other elements to 0. When all the elements are to be initialized to 0, the following short cut method may be used. static int m [3] [5] = {{0}, {0}, {0}}; The first element of each row is explicitly initialized to 0 while other elements are automatically initialized to 0. While initializing an array, it is necessary to mention the second (column) dimension, whereas the first dimension (row) is optional. Thus, the following declarations are acceptable. static int arr [2] [3] = {12, 34, 23, 45, 56, 45}; static int arr [ ] [3] = {12, 34, 23, 45, 56, 45 }; Memory Representation of Two Dimensional Array In memory, whether it is a one dimensional or a two dimensional array, the array elements are stored in one continuous chain. The arrangement of array elements of a two dimensional array of students, which contains roll numbers in one column and the marks in the other (in memory) is shown below:
  • 5. 1234 5002 5004 5006 5008 5010 5012 5014 5016 Value Address 1234 1234 1234 1234 1234 1234 1234 S[0][0] S[0][1] S[1][0] S[1][1] S[2][0] S[2][1] S[3][0] S[3][0]Notation 1234 5002 5004 5006 5008 5010 5012 5014 5016 Value Address 1234 1234 1234 1234 1234 1234 1234 S[0][0] S[0][1] S[1][0] S[1][1] S[2][0] S[2][1] S[3][0] S[3][0]Notation e.g.: 1. Program that stores roll number and marks obtained by a student side by side in a matrix main( ) { int stud [4] [2]; int i, j; for (i = 0; i < = 3; i++) { printf ("n Enter roll no. and marks"); scanf ("%d%d", &stud [i] [0], &stud[i] [1]); } for (i = 0; i < = 3; i++) printf ("%d%dn", stud [i] [0], stud [i] [0]; } There are two parts to the program, in the first part through a for Loop we read in the values of roll number and marks, whereas in second part through another for Loop we print out these values. Multi-dimensional Array C allows arrays of three or more dimensions. Multi-dimensional arrays are defined in much the same manner as one-dimensional arrays, except that a separate pair of square brackets is required for each subscript. The general form of a multi-dimensional array is data_type array_name [s1] [s2] [s3] . . . [sm]; e.g.: int survey [3] [5] [12]; float table [5] [4] [5] [3]; Here, survey is a 3-dimensional array declared to contain 180 integer_type elements. Similarly, table is a 4- dimensional array containing 300 elements of floating point type. An example of initializing a 4-dimensional array: static int arr [3] [4] [2] = {{{2, 4}, {7, 8}, {3, 4}, {5, 6},}, {{7, 6}, {3, 4}, {5, 3}, {2, 3}, }, {{8, 9}, {7, 2}, {3, 4}, {6, 1}, } }; In this example, the outer array has three elements, each of which is a two dimensional array of four rows, each of which is a one dimensional array of two elements.