Advanced C Programming
Chapter 1
Arrays
,Arrays
• 1.1 Array definition, declaration, initialization
• 1.2 Advantages and Disadvantages of arrays
• 1.3 Types – one, two and multidimensional,
array slice
• 1.4 Memory representation of Arrays
• 1.5 Passing arrays to functions
, Why we need Array in C Programming?
Consider a scenario where you need to find out the
average of 100 integer numbers entered by user. In C,
you have two ways to do this:
1) Define 100 variables with int data type and then
perform 100 scanf() operations to store the entered
values in the variables and then at last calculate the
average of them.
2) Have a single integer array to store all the values, loop
the array to store all the entered values in array and
later calculate the average.
Which solution is better according to you? Obviously
the second solution, it is convenient to store same data
types in one single variable and later access them using
array index
, Array definition
Arrays are referred to as structured data types.
An array is linear data structure which consists of finite
ordered collection of homogenous data such as:
• elements are stored in continuous memory locations.
• Each element can be accessed by using subscript or
index.
Here the words,
• finite means data range must be defined.
• ordered means data must be stored in continuous memory
addresses.
• homogenous means data must be of similar data type.
Example where arrays are used,
• to store list of Employee or Student names,
• to store marks of students,
• or to store list of numbers or characters etc.
Chapter 1
Arrays
,Arrays
• 1.1 Array definition, declaration, initialization
• 1.2 Advantages and Disadvantages of arrays
• 1.3 Types – one, two and multidimensional,
array slice
• 1.4 Memory representation of Arrays
• 1.5 Passing arrays to functions
, Why we need Array in C Programming?
Consider a scenario where you need to find out the
average of 100 integer numbers entered by user. In C,
you have two ways to do this:
1) Define 100 variables with int data type and then
perform 100 scanf() operations to store the entered
values in the variables and then at last calculate the
average of them.
2) Have a single integer array to store all the values, loop
the array to store all the entered values in array and
later calculate the average.
Which solution is better according to you? Obviously
the second solution, it is convenient to store same data
types in one single variable and later access them using
array index
, Array definition
Arrays are referred to as structured data types.
An array is linear data structure which consists of finite
ordered collection of homogenous data such as:
• elements are stored in continuous memory locations.
• Each element can be accessed by using subscript or
index.
Here the words,
• finite means data range must be defined.
• ordered means data must be stored in continuous memory
addresses.
• homogenous means data must be of similar data type.
Example where arrays are used,
• to store list of Employee or Student names,
• to store marks of students,
• or to store list of numbers or characters etc.