In C and C++, and array is a block of memory reserved to hold an ordered list of data of the same type. This could be a list of type int, float, double etc.. It can also be a list of other arrays, or more complex types such as structures and unions.
For example:
#include <stdio.h> int primes[] = {2,3,5,7,11,13,17,19,23,29}; int main() { for (unsigned int n=0; n<10; n++) { printf("%dth Prime is %d\n", n, primes[n]); } return 0; }
Let’s look at this code. First we see the array “primes”. It is a list of integers (type int). This array is already initialized with values.
int primes[] = {2,3,5,7,11,13,17,19,23,29};
These integer values will be stored adjacent to each other in memory. If the size of an int is 4 bytes (on a 32bit microcontroller), the the array will occupy a block of 40 bytes.
Next, we iterate through and read the values in the array using a for-loop:
for (unsigned int n=0; n<10; n++) { printf("%dth Prime is %d\n", n, primes[n]); }
For each value of n, we read the nth value primes[n]
Now consider a more complex example:
#include <stdio.h> #include <math.h> #define N 1000 double x1[N]; double x2[N]; double delta[N]; //Initial values - make these very similar double initialValue1 = 0.3; double initialValue2 = 0.299999999; //The parameter r, set to generate a chaotic sequence double r= 4.0; int main() { //****************************************** //Let's generate a chaotic sequence of data! //****************************************** // https://en.wikipedia.org/wiki/Logistic_map //Start with very similar initial conditions x1[0] = initialValue1; x2[0] = initialValue2; delta[0] = 0.0; //Calculate values for n=1..99 and any differences //using the same "logisic map" function for (unsigned int n=0; n<(N-1); n++) { x1[n+1] = r*x1[n]*(1.0-x1[n]); x2[n+1] = r*x2[n]*(1.0-x2[n]); delta[n+1] = x1[n+1] - x2[n+1]; } //Write results for (unsigned int n=0; n<N; n++) { printf("n=%d\t%8.6f\t%8.6f\tDIFFERENCE=%8.6f\n", n, x1[n], x2[n], delta[n]); } return 0; }
First of all, we define N as an alias for 1000
#define N 1000
Now we create three arrays of type double.
double x1[N]; double x2[N]; double delta[N];
Each of these is a distinct block of memory, size N*sizeof(double) . The first values each array is then initialised:
x1[0] = initialValue1; x2[0] = initialValue2; delta[0] = 0.0;
Note that the index (value in square brackets) starts at zero for C and C++ arrays.
(For the curious, we see initialValue1 and initialValue2 have very similar values as this is a demonstration of “chaotic behaviour”). Next we once again use a for-loop to iterate, calculate some values and store results in the arrays:
for (unsigned int n=0; n<(N-1); n++) { x1[n+1] = r*x1[n]*(1.0-x1[n]); x2[n+1] = r*x2[n]*(1.0-x2[n]); delta[n+1] = x1[n+1] - x2[n+1]; }
The value of n ranges from 0 to 98. Once the arrays are full, we write the values to the terminal:
for (unsigned int n=0; n<N; n++) { printf("n=%d\t%8.6f\t%8.6f\tDIFFERENCE=%8.6f\n", n, x1[n], x2[n], delta[n]); }
Note the data types:
When we use square brackets to access the nth value in an array, such as x[n] , we say we are dereferencing the array.
When we write the array name without any square braces, we get the memory address of the first element in the array. This is also known as a reference to the first element of the array. Therefore we call x1 a reference type.