The Array Structure
• A linear sequence of memory cells, each accessible by an an integer index, ranging from 0 to the number of cells minus 1
• Like Python lists and Java array lists, C arrays support random (constant-time) access to the cells
int integers[10]; // Cells for 10 intschar letters[5]; // Cells for 5 charactersfloat matrix[3][2]; // A 3 by 2 matrix of floats
int max = 100; // The number of cells in the next arraydouble doubles[max]; // Cells for max doubles
Declaring Array Variables
The syntax
<element type> <variable> [<integer expression>]
declares an array variable and reserves memory for a definite number of cells (use more [] for more dimensions), each of which can store an element of the given type (arrays are statically typed)
Each cell initially contains garbage!
Initializing and Processing
int i, max = 10, array[max]; // Declare variables
for (i = 0; i < max; i++) // Store 1..max in the array array[i] = i + 1;
for (i = 0; i < max; i++) // Print the contents printf("%d\n", array[i]);
Here we initialize all of the array’s cells and visit them all
The loop is pretty standard
Initializing Arrays that Are Not Full
int number, length = 0, max = 10, array[max];
printf("Enter a number or 0 to stop: ");scanf("%d", &number);while (number){ array[length] = number; length++; if (length == max){ printf("Maximum number of numbers entered\n"); break; } printf("Enter a number or 0 to stop: "); scanf("%d", &number);}
The number of data values is tracked with a separate variable
This length variable is also used in processing the array later
Processing Arrays that Are Not Full
int number, length = 0, max = 10, array[max];
// Get the array’s data and length here
// Print all of the currently available valuesint i;for (i = 0; i < length; i++) printf("%d\n", array[i]);
The number of data values is tracked with a separate variable
This length variable is also used in processing the array later
Arrays and Functions
#include <stdio.h>
#include "numbers.h" // Use a library of array processing functions
int main(){ int max = 100; int numbers[max]; int length = getNumbers(numbers, max); if (length > 0) printf("The average is %f\n", sum(numbers, length) / (double) length); else printf("No numbers were entered\n");}
An array can be passed as an argument to a function
The function can access or replace values in the array cells
Example: Find the Minimum
// Returns the index of the minimum value in the arrayint minIndex(int array[], int length){ int probe, minPos = 0; for (probe = 1; probe < length; probe++) if (array[probe] < array[minPos]) minPos = probe; return minPos;}
The element type of the array parameter must be specified, but its physical size need not be
Therefore, this function can process an integer array of any physical size
The logical size (length) is passed too, because the array might not be full
How Parameters Are Passed to Functions
• Parameters of basic types (char, int, float, and double) are passed by value (a copy of the value of the argument is placed in temporary storage on the runtime stack)
• A copy of an array’s base address is also placed in temporary storage, so its cells can still be accessed or modified