An array a collection of similar data elements grouped under one name. A variable is scalar if it contains a single dimensional value. You can also say that a scalar variable only represents magnitude. A variable containing an array is considered vector because it contains a list of single dimensional values.
You can access an value of an existing array using the index of that value, which is the location of that value in the array. As an example, A[2]=15;
would set the value of 15 into the third position (Array indexes start at 0) in the array of variable A
.
Arrays are declared using brackets: []
. An array needs to be declared as a variable so that it can be referenced. In C, arrays also require a data type and a length in the array declaration.
int A[5];
In this example, an array A
is declared as an integer array that is has a length of 5. This means that the array has 5 available addresses to store data in. At the time of this declaration (run time), garbage data will occupy those addresses.
You can initialize an array using curly braces: {}
.
int A[5] = {1,2,3,4,5};
This will create and populate the array A
with values: 1 2 3 4 5
.
In integer arrays, if you supply less values than the length of the array, the rest of the indexes will contain 0s.
int A[5] = {1,2};
This will create and populate the array A
with values: 1 2 0 0 0
.
If you do not specify a length, the array will be as long as your initialization.
int A[] ={1,2};
You can update/change the value of an array by declaring the index of that value as something else.
A[3] = 6;
You can access all of the indexes of an array using a for loop.
for (i=0; i<5; i++) {
printf("%d", A[i]);
}
There are also several ways of accessing a single value:
printf("%d", A[2]);
printf("%d", 2[A]);
printf("%d", *(A+2));
These will all print the same value.
When using static arrays, the size of the array must be constant. The size of the array must be known at run-time of the compiler. Once the size of the array is known by the compiler, it cannot be changed.
// Array created in the stack
int A[5];
Using pointers you can create an array in the heap. This will make the array dynamic. It requires the use of the malloc
function to work in the C language. In the heap, it is possible to change the size of the array.
#include <stdlib.h>
// Array created in the heap
int *P = (int *)malloc(5*sizeof(int));
To change a value in the array, you can use the pointer variable as the name of the array:
P[0] = 5;
Whenever an array is created in the compiler, it will allocate space in the memory. In order to recycle that space again, you will have to free the memory from that space allocation. You can do this with the
free
function.
free(P);
So long as the array is in the heap, there are a couple of ways to change the size of the array.
One method is to define another array of a larger size, copy the values from the initial array into the new one, remove the initial array from memory and point it to the new array; and then nullify the variable for the new array. This will look like the size of the array changed, but in reality, the pointer was pointed to a larger array with the same values and the previous one was destroyed.
// Initial array of size 5
int *P = (int *)malloc(5*sizeof(int));
// New array of size 10
int *Q = (int *)malloc(10*sizeof(int));
// Copy values from P into Q
for(i=0;i<5;i++) {
Q[i] = P[i];
}
// Remove Array P from memory
free(P);
// Point P to Q
P = Q;
// Remove Q as a variable
Q = NULL;
There are 3 methods for creating a 2-dimensional array.
int A[3][4];
A = {{1,2,3,4},{2,4,6,8},{3,5,7,9}};
Though this may look like a 2 dimensional array, the address are in fact linear. The table below would show hypothetical address locations for an array created this way.
To the memory, this array is created in a single dimension with 12 integers. Though this is technically a single dimension array, the compiler allows you to access the values in a second dimension.
A[1][2] = 15;
This will change the value of the second row, third column to 15.
int *A[3];
A[0] = (int *)malloc(4*sizeof(int));
A[1] = (int *)malloc(4*sizeof(int));
A[2] = (int *)malloc(4*sizeof(int));
This will create an array of size 3 in the stack with each value pointing to an array of size 4 in the heap. The end result is a 2 dimensional array.
To add/change/update a value, you can use the same method as you would in a static array.
A[1][2] = 15;
int **A;
A = (int **)malloc(3*sizeof(int));
A[0] = (int *)malloc(4*sizeof(int));
A[1] = (int *)malloc(4*sizeof(int));
A[2] = (int *)malloc(4*sizeof(int));
Unlike the second method for creating a 2 dimensional array, this method ensures that the entire structure of the array is completely located in the heap.
And again, to add/change/update a value, you can use the same method as you would in a static array.
A[1][2] = 15;
As long as you know the lengths of both the outer and nested arrays, you can use nested for loops to access all of the indexes in an array.
for (i=0;i<3;i++) {
for (j=0;j<4;j++) {
printf("%d ", A[i][j]);
}
}