Arrays In C



In the previous, we discussed the importance of primitive data types which are basic data types used to build derived data types. In this article, I going to explain the array which is one of derived datatype

Arrays are nothing but a collection of variables or when variables are grouped together we obtain a derived data type ie but an array.

An array can be of any data type. let us look at integer one data type
Integer array-
These type of array holds a group of integer variables.syntax for declaration of an array


In the above example, you can see an array of integer type holds 10 variables of int type only.see the following syntax for initialization of the array-

If you don't understand the first way of initialization, don't worry still we did not discuss loops concept .we will discuss in later topics. The first method mainly used for taking input from the user, we will discuss it in loops topic.it is a dynamic way of initializing the array.
When we come to the 2nd method, we are statically initializing the array which is not good practice. We are manually giving input in the program itself and input from the user is not taken.so it is not a good idea to initiate the array in this way.
So we have seen two ways of initializing an array
1.The dynamic way of using loops
2.static way by giving input in the program itself.
when data gets stored in an array, we can retrieve the data using the index, so index in an array starts from '0'(zero) and ends at (n-1).
Let's take an example if you are storing values 1,2,3 into an array, so value 1 gets stored at 0th index and its address will say '122', in similar way value 2 gets stored at 1st index and its address will be '124'(as integer takes 2 bytes), in similar way value 3 stored at 2nd index and its address will be '126'.so whenever the user gives first input into an array it's gets stored at 0th index at some location in memory, and then the 1st index continues till the (n-1) index. I think now you understood the for every member in an array a position(index) is given.with the help of that index only we can retrieve the data members of an array. now look at following code how much memory allocated for an array

As you see above integer takes 2 bytes, so for an array of 10 variables, it takes 40 bytes of memory.so it's not definitely that every compiler takes only 2 bytes for allocating memory for int.some compiler takes 4 bytes also.so now you understood array concept I think if you have any doubts comment below

Post a comment

0 Comments