An array is a collection of similar data which is stored in sequential order in the memory. For example, if we save one integer variable into memory it will occupy 4 bytes. If we make another variable then it also occupies 4 bytes, but this memory of two variable may be consecutive or maybe not. This type of variable declaration method is alright until we are having less number of variables.
Once we have to handle the number of variables, it’s maybe 50, 100 then making 50 variable is very difficult.
We have a solution, we can make an array of N number of the element. In the array, all variable will be saved into consecutive memory locations.
Declaration of array
array stored in memory.
This will reserve 50th places each of size 4 bytes int memory and the good part is all are consecutive in memory locations.
You can initialize an array in C either one by one or using a single statement as follows
marks=47;, marks=48; marks=45;, marks=44;,marks=49.
How to access this element? This is very simple,
We can access each element individually by showing their place value.
E.g. array, array, array etc.
Keep remember array always starts from 0, means the first element is always array and last-element is one less than the maximum number element, in our case array.
Let’s see the program using the array which explains the declaration, assignment and accessing the array;
Int a ; /* a is array of 12 integers*/
Int I,j; /* I and j are integers*/
/*initializing the values to array elements a */.
a[i]=i+10; /* assigning value i+10 to ith element*/
/* printing the values of elements of array a*/
Passing array to function
We can pass array elements to function in two ways. One is bypassing a single value or by passing the full array.
Passing single value to array
Void foo(int a)
Int main (void)
foo(array); // array is passed to the called function foo
Passing Entire Array to Function
Sum=add(a); // passing full array to called function
Int add(int a)
Int sum=0, i;
return sum;// return sum to main function.