Course Introduction to Programming
Arrays
In this lesson we will learn what they are and how to use arrays, addressing the following topics:
- What is an array?
- What is an array used for?
- Arrays in C
- How to access the elements of an array?
- How to assign values to the elements of an array?
- How to read the elements of an array?
- Example
What is an array?
Arrays store a set of variables.
In C, the arrays are homogeneous. That is, the variables stored by an array are all of the same type.
What is an array for?
An array is generally used to create several variables of the same type in a simple way.
We can also use arrays to automate tasks.
Arrays in C
The declaration of an array in C follows the template below:
<type> <name>[<size>];
To make it easier to understand, analyse the example below:
/*
type -> int (stores an integer)
name -> array
size -> 3 (stores three elements)
*/
int array[3];
When declaring an array, the computer will reserve a set of sequential memory positions.
This means that in the example above, the computer has reserved three sequential positions capable of storing integers.
Observations:
The C-arrays are the same size from their declaration to the end of the program.
We can use the sizeof command to know the number of bytes of an array. If we want to know how many elements it actually has, we can just divide the bytes size of the array by the bytes size of the array type:
#include <stdio.h>
int main(){
int v[5];
printf("Size in bytes of a integer -> %d\n", sizeof(int));
printf("Size in bytes of array v -> %d\n", sizeof(v));
printf("Quantity of elements in array v -> %d\n", sizeof(v)/sizeof(int));
}
/* Output:
Size in bytes of a integer -> 4
Size in bytes of array v -> 20
Quantity of elements in array v -> 5
*/
How to access the elements of an array?
The name of the array is treated as a pointer to the first memory position reserved for it.
To access each position of the array, we can use both the arithmetic of pointers and the operator [ ], indicating between brackets which position we wish to access.
As an example, let's analyse the access to the elements of the array int x[4] :
*x | *(x+1) | *(x+2) | *(x+3) |
---|---|---|---|
x[0] | x[1] | x[2] | x[3] |
Note that when using the [ ] operator, the first position of the array is given by the
Thus, an array with a size of
Observation: the arithmetic of pointers works because the set of positions reserved for the array is sequential. For ease, see the code output below:
#include <stdio.h>
int main(){
int v[3];
/*
As the size of an integer is 4 bytes, the distance between
two consecutive positions in the array v should also be 4 bytes
*/
printf("First position -> %d\n", &v[0]);
printf("Second position -> %d\n", &v[1]);
printf("Third position -> %d\n", &v[2]);
}
/* Output:
First position -> 6422292
Second position -> 6422296
Third position -> 6422300
*/
How to assign values to the elements of an array?
There are two ways to assign values to the elements of an array:
- Changing all the elements of the array (the only method that can be used as you declare the array):
int v[5] = {1, 2, 3, 4, 5}
/*
v[0] = 1
v[1] = 2
v[2] = 3
v[3] = 4
v[4] = 5
*/
v = {5, 4, 3, 2, 1};
/*
v[0] = 5
v[1] = 4
v[2] = 3
v[3] = 2
v[4] = 1
*/
Observations:
- If we state all the elements of the array in the declaration, we can omit the size, as it will adjust to the number of elements present inside the keys:
int x[] = {1, 2, 3}; // x has 3 elements
int y[] = {12, 15, 18, 21}; // y has 4 elements
- If the number of elements within the keys is lower than the number of elements the array can hold, then the positions at the end of the array will be filled with
(in the case of numeric arrays):
int t[5] = {1, 2, 3};
/*
t[0] = 1
t[1] = 2
t[2] = 3
t[3] = 0
t[4] = 0
*/
- To initialize an array with the value
in all its positions, we can use the observation described above and not place any elements inside the keys:
int z[3] = {};
/*
z[0] = 0;
z[1] = 0;
z[2] = 0;
*/
- Changing only one element:
int v[5];
v[0] = 7; // first array position
v[4] = 9; // fifth array position
*(v+2) = 123; // third array position
How to read the elements of an array?
To read the elements of an array, we can use the scanf command in the following two ways:
- Using the ampersand ("&") and the operator [ ] indicating the position of the array:
int v[7];
scanf("%d", &v[3]); // read the value to be placed in the fourth position of the array
/*
The use of the ampersand is therefore necessary when using the operator [] as we are not taking the address of the element, but accessing the element in fact
*/
- Using the pointers arithmetic, without the ampersand:
int v[7];
scanf("%d", v+2); // read the value to be placed in the third position of the array
/*
The use of the ampersand is therefore not necessary when using pointers arithmetic, as we are taking the address of the element, because the array's name is a pointer to its first position
*/
Example
To better illustrate the knowledge learned so far, analyse the example below:
#include <stdio.h>
int main(){
int v[5];
for(int i=0;i<5;i++){
scanf("%d", &v[i]);
}
for(int i=0;i<5;i++){
if(*(v+i) % 2 == 0){
printf("The number in the %d position of the array is even!\n", i);
}
other{
printf("The number in the %d position of the array is odd!\n", i);
}
}
}
Note that the above code uses a for loop to go through all the indices of our array and read their respective elements using the scanf command.
After that, we again use a for loop to scroll through all the elements of the array and check, one by one, whether they are even or odd.
Now you have learned what they are and how to use arrays, use your creativity to create other programs and put your knowledge into practice.
Until the next class!