Array is a collection of elements of similar data type stored at contiguous memory location. An array can be of any type for example: int, char, float, double etc. like int array holds the elements of int types while the float array holds the elements of float types.
OR simply you can say, Array is a group of variables of same data type stored at contiguous memory location.
It also has the capability to store the collection of derived data type such as pointers, structure etc. Elements can be accessed randomly using indexes of an array (index is also known as subscript).
Note: Each values stored in array is called an elements of the array. The elements of the array share the same variable name but each elements has its own unique index number. As you can see in above diagram.
Why do we use an array?
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: first: you need to 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. Second: you need to declare a single integer array to store 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 one is better, it is convenient to store same data types in one single variable and later access them using array index. i will discuss about array index later.
Declaration of Array:
In order to declare an array you need to specify as follow:
- The data type of an array elements. It could be int, char, float, double etc.
- The name of an array.
- A fixed number of elements that array may contain. The number of elements is placed inside the square brackets followed by the array name.
Syntax
DataType ArrayName [ArraySize];
For Example: Array declaration by specifying size
/*Array declaration by specifying it's size*/
float marks[5];
int num[6];
/* An array can also be declared by user specified size */
int x = 6;
int num[x];
There are many ways to declare an array see at below declaration with initialization.
Similarly an array can be of any data type such as int, char, float, double etc.
In above declaration, marks is a name of an array of float type, and five in the square bracket [5] represent the length of the array. Similarly in the 2nd declaration, num is a name of an array of int type, and six inside the square bracket [6] represent the length of an array.
Declaration with initialization:
Array initialization is the process of assigning elements to an array. The initialization can be done in a single statement or one by one.
1) Arrays may be initialized when they are declared, just as any other variables. Place the initialization data in curly {} braces followed by the equal (=) sign. Let's see the below example.
/* An array can be initialized when they are declared,
*just as any other variables. */
int num[6] = {2, 4, 6, 8, 10, 12};
For Example: Declaration of array
In above, int num[6]; is a declaration statements of an array, and number inside the curly braces {2, 4, 6, 8, 10, 12}; are initialized values, these values will store in array at contiguous memory location. As you can see in above diagram.
Note: The first element in an array is stored at index [0], while the last element is stored at index [n-1], where n is the total number of elements in the array.
2) An array can also be initialized by providing fewer data elements than the size of the array. And the remaining array will be automatically initialized to zero. Have a look at the below example.
/* An array can also be initialized by providing fewer data elements than
* it's size. And the remaining array will be initialized
* to zero. Have a look at below example. */
int num[6] = {2, 4, 6};
See the above diagram to understand it better.
3) If an array is to be completely initialized, then the dimension of the array is not required. The compiler will automatically size the array to fit the initialized data.
For Example: If the number of initialized values are nine {2, 4, 6, 8, 10, 12, 14, 16, 18}; then the compiler will fits it's size to 9 automatically, and if the number of initialized values are six then fits it's size to 6. See the above diagram to understand it better.
/* Dimension is not required, If an array is to be completely initialized.*/
int num[] = {2, 4, 6, 8, 10, 12, 14};
Note: You can initialized any number of elements when the array size is not specified.
4) Array can be initialized individually too.
/* Declaration of an array */
int num[3];
/* Individually initialization */
num[0] = 10;
num[1] = 20;
num[2] = 30;
5) An array cannot be initialized with more elements than it's size. If you do like this you get a compilation error "too many initialization". See the above diagram to understand it better.
/* An array cannot be initialized with more elements than it's size.
* If you do, you get a compilation error "too many initialization".*/
int num[5] = {2, 4, 6, 8, 10, 12, 14};
What if, an array is not initialized?
If array is declared in a function, then the values is undefined(garbage value). If the array is declared as a global one or as static in a function, then all elements are initialized to zero if they are not initialized already.
Advantage of array:
- Array elements can be access randomly using array index
- Use less line of codes it creates a singal array of multiple elements.
- Easy access to all elements.
- Traversal through the becomes easy using a singal loop.
- Sorting becomes easy as it can be accomplished by writing less line of code.
Disadvantage of array:
- An array in C is not dynamic, as it allows fixed number of elements to be entered which is decided at the time of declaration.
- Insertion and deletion of elements can be costly since elements are needed to be managed in accordance with the new memory allocation.
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.