Necessity for array
C language has different data types and variables to hold rutime parameter. One variable is to store one runtime parameter. Now if there are multiple values to store the we can take multiple variables. Here we are storing roll no. of students in integer type variables.
Variable Driven Approach
We have taken three variables to store roll no. of three students. This approach is not scalable and variables are also scattered and can be accessed by their individual names. If there is any facility to group these variables inside one entity and individual elements can be accessed via an index then this situation would be more manageable. C language already has a feature called array to deal with this situation. Array can hold any number of elements and it is accessed through a single variable name.
Definition
Array is a collection or sequence of a same data type objects inside a logical variable name where individual objects can be accessed by an index.
Array is a grouping of same type of variables of either C language defined types or an user defined type. Array is very useful when dealing with same type of variables. Each individual entity of an array is called element. Elements are placed sequentially next to next in the memory. Thus total size of the entire array is the sum total of size of all elements.
Array Driven Approach
Array Elements in memory
Array is a sequence of variables called elements placed in memory in accending order. It start with index = 0 which is element 1 and ends with index = n-1 with is element N.
An array of integer with N elements
There should be a defined number of elements in an array thus it is mandatory to give the element count while defining an array. This element count can be a macro of a positive integer or a constant variable defined earlier. Now we will discuss more on this with examples.
Size of Array
Array is a sequence of elements in memory. Thus it will give the size which is equal to the size of individual element times of the number of elements. Same way we can get the element count if we divide the size of array by the size of each element.
Array initialization
Array can be declared in global,Local and static scope and the rule for initial value is same as it is applicable to normal variables. This is reason array elements are initialized before they are accessed in runtime.
Array elements access for writing
We are assigning roll no. to the student's record as they have enrolled.
Array elements access for reading
We are showing student roll no. as they are enrolled.
Here array has element range from 0 – (n-1) range and it is linear in a single Dimension. Now there are possibilities to have further groupings inside each element or in simple words each can have another dimension. Thus there comes multidimensional array. We will discuss more in our next section.
About our authors: Team EQA
You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.