C – Arrays
Arrays are different from primitive types like integer,chars. Arrays provides fixed length sequential data. All of the data is same type. We can think array like a same type list. Arrays uses indexes to specify specific item in array.
There is two must do to define array. One is the type of the array and second is length of the array. Basic syntax of defining array is like below;
For example if we want to define an integer type array with 10 items we can use following code.
As another example to define and float array to store average temprature for 31 days we can use following array
After defining arrays according to their types and sizes some storage is allocated in memory. So while developing C programs array types and sizes must be set cautiously because it can creates low memory problems.
Initializing means setting values to the newly created array. Arrays in C can be initialized in different ways.
Loop Through Arrays
As we know arrays are sequential data types. Looping through arrays are very popular and useful way to consume array data. We will look some examples about how to enumerate array data with loops.
Arrays are accessed with index numbers. Arrays those have single line of data are named single dimension arrays. Arrays can be used int matrix or similar structure operations without using multiple arrays. More than one line of arrays are called multi dimension arrays.
As we see we add new brackets according to dimensions. If there is more than 2 dimensions we will add more [size] brackets.