Arrays in C Programming
Last Updated Nov 20, 2015, 07:00:14 PM
The memory allocation for array elements is contiguous, meaning the array elements are stored without any gaps in the memory. The array data structure is very helpful when dealing with large data sets. Every element in an array can be accessed with its unique location called as index.
Each element in array is stored with its own location to access them effeciently. The memory allocation starts at Zero and ends at N-1. Meaning, if we have 5 arrays elements, the indexing starts at
0 and ends at
4. Here the index of first element would be 0, and index of second element would be 1 and so on....
As a programmer, it your responsebility to declare the array before you use it. To declare an array we first need to know two things, what type of array we need for example, if we are working with values and numbers we can declare the array as
int, double, if we are working with data sets as names, and characters we may declare the array type as
string, char. Second we need to give a proper name to the array. It is a best practice to give descriptive name.
While declaring an array, we can also give the array size if we know exactly the size of the data we are going store, or we can keep it empty. No problem in either way. Also make sure, you close the array declaration with a semicolon
Always, make sure the array size must be of integer. Let us see an example of declaring an array in c programming.
Let us say, we want to store all the marks of ten people here the array size would be 10
In the above example, the array name is marks and the array size is 10
How to Access Array Elements
As we already know an array contains collection of similar elements and these elements are stored with its unique indexes which makes the our life easier to locate any element. To access any element in array, we can access the element by its index. In the below example, we have an array called
makrs Which can stores marks upto 10. Now let us access these elements
In the above example, we have an array called marks which is of size 10. So we stored the marks upto 9 as the index starts from 0
Now let us say, we want to access the values of element 4 and element 6.
Here is the final example of the above programTry It Now
Initializing Array Elements at the time of Declaration Itself
Arrays can contain data, and we can assign that data during array initialization. However, we can also assign the same data during the declaration as well. See the below exampleTry It Now
Initializing Array with a String - (Method 1)
Strings in C are series of characters followed by a null type which indicates the end of the sting in C.Try It Now
In the above example, we wrapped the entire string in double quotes and stored that string in an array called str
Initializing Array with a String - (Method 2)
In this method, we need to initialize each character in single quotes and ends the string with null type which indicates the end of the string. It indicates the compiler to terminate thereTry It Now
Other Array Topics you might want to learn
Types of Arrays in C
Single Dimensional Arrays in C
Two Dimensional Arrays in C