Objective![]()
|
LessonIntroductionC provides arrays to store a series of elements of one data type. Let us start with an analogy. Suppose that you have a cupboard in your house. You would like to keep books on each shelf of the cupboard. It would be nice to keep books related to one subject on one rack, books of another subject on another rack. A rack is an array. A rack is used to store books of the same subject. Similarly, an array can store data of the same type. When you declare an array, you must specify the type of data this array will hold. Every element of the array must be of that data type. The syntax for declaring an array is: data_type array_name[size];
For example: int x[5];
Is an array of integers, with size 5, called x. Similarly, float y[6];
is an array of floating point numbers, with size 6, called y. To access the elements of an array, you must use array notation. The first element of an array is at position 0, the second at position 1, etc. The last element of an array is at position size - 1. x[1] = 10;
As you can see, once you have selected an element using array notation, you can manipulate that element as you would any variable. Here is a more complex example: int z[2];
z[0] = 10;
z[1] = 50;
z[0] = z[0] + z[1]; /* z[0] now contains the integer 60 */
Array Initialization IWhen an array is declared, it is initially 'empty'; it does not contain any values. We can initialise an array as follows: int x[5] = {5, 7, 2, 3, 8};
int x[] = {5, 7, 2, 3, 8};
When have created an array with 5 elements. The first element is 5, the second is 7, etc. Array Initialization IILet us look at a small code snippet that prints the number of days per month. #include<stdio.h>
#include<conio.h>
#define MONTHS 12
int main(void)
{
int days[MONTHS] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int i;
for(i = 0; i < MONTHS; i++)
{
printf("Month %d has %d days.\n", i + 1, days[i]);
}
return 0;
}
OUTPUT Month 1 has 31 days. Month 2 has 28 days. Month 3 has 31 days. Month 4 has 30 days. Month 5 has 31 days. Month 6 has 30 days. Month 7 has 31 days. Month 8 has 31 days. Month 9 has 30 days. Month 10 has 31 days. Month 11 has 30 days. Month 12 has 31 days. (You may not have seen If you lack faith in your ability to count, we can let the computer give us the size of an array, by using for(i = 0; i < MONTHS; i++)
with for (i = 0; i < sizeof days / sizeof (int); i++)
Assigning Array ValuesWe can assign values to array members by using array index. int x[5];
int i;
for(i = 0; i < 5; i++)
{
x[i] = i + 1;
}
Thus the array Passing Arrays to a FunctionArrays cannot be passed to functions. If an array name is used as an argument in a function call, the address of the first element of the array is passed. The function can access the array through that address. Suppose we want to write a function that returns the sum of elements of the array. #include <stdio.h>
/* prototype */
int sum(int a[]);
int main(void)
{
int marbles[] = {5, 6, 2, 3, 7};
int ans;
ans = sum(marbles);
printf("The total number of marbles: %d", ans);
return 0;
}
int sum(int arr[])
{
int i, s = 0;
for(i = 0; i < 5; i++)
{
s = s + arr[i];
}
return s;
}
Note that the array name How Are Arrays Stored in Memory?When we declare an array, space is reserved in the memory of the computer for the array. The elements of the array are stored in these memory locations. The important thing about arrays is that array elements are always stored in consecutive memory locations. We can verify this fact by printing the memory addresses of the elements. (Just like every person has a street address, every location in the memory has a memory address, usually a number, by which it can be uniquely identified.) #include<stdio.h>
int main(void)
{
int a[] = {1,2,3,4,5,6,7,8,9,10};
int i;
/* Print the Addresses */
for (i = 0; i < 10; i++)
{
printf("\nAddress of a[%d] : %p", i, (void *) &a[i]);
}
return 0;
}
OUTPUT Address of a[0] : ffe2 Address of a[1] : ffe4 Address of a[2] : ffe6 Address of a[3] : ffe8 Address of a[4] : ffea Address of a[5] : ffec Address of a[6] : ffee Address of a[7] : fff0 Address of a[8] : fff2 Address of a[9] : fff4 As we can see from the output, the elements are stored at ffe2, ffe4, ffe6, etc. You might be wondering why the numbers are not consecutive. The reason is very simple. The size of the If instead we declared an array of As you can see, the elements are consecutive. This concludes the lesson on arrays. |
Assignments![]() |
|