Arrays Flashcards
what is an array
single contiguous range of memory that holds multiple instances of a single
specified data type.
array memory
divided into cells of equal size
each cell has the right size to hold a specific
e.g array of int,divided into 4 byte cells
Bytes is an array equation
B = sizeof(T) *N
the size of the data type
instances stored in the array, sizeof(T), and the number of elements in the array, N:
N = B/sizeof(T)
Formula: address of element at index i
address of element at index i
(address of element at index 0)+(i sizeof(T))
Array size
the number of elements in an array. is fixed
where we can not query their size at runtime
Thus, for every array, we need to somehow record its size.
we use either macros or constants.
Macros
define NUM_ELEMENTS 12
not a variable
not a constant
is a value before compile time
This is done by a program called the C pre-processor.
commands start with a # and they are called pre-processor directives
no ,”=” nor data type Declaration
macro names have all capital letters to distinguish them from variables.
Constants
const size_t num_elements = 12;
constant works like a variable,
including the const keyword causes any code that tries to directly change its value to produce a compiler error to inform
you that an undesired attempt is being made to modify the value.
used the type size_t here because we are storing the size of something. While it is tempting to use type int here, use
size_t for storing the sizes of data
allocating constants
constants are actually variables and are allocated like any other variables using
the standard C allocation methods — static, or automatic.
Statically-allocated constants (e.g.
static const int num_elements=12;) are created at load-time and are stored in the init.data
area.
Automatically-allocated constants, for example, a const variable defined in a function, are
allocated on the stack at runtime when they come into scope.
Defining an array
<data> <identifier>[< number of elements >];
</identifier></data>
Define an array of 12 using macro
Define an array of 12 using constant
Define an array of 12 using a variable (discouraged)
DO NOT USE VARIABLE
initialize array
int coolNumbers [ num_elements ] =
{42 , 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0};
The number of elements in the initializer must be exactly equal to the number of elements specified between the square brackets, and the must be of a compatible type.
the data type of array
int coolNumbers [ num_elements ] = {42 , 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0};
the actual type of the array in int[12]
as soon as we use the array, coolNumbers is implicitly converted to type int*, a pointer to the first element of the array.
This process is called decay, which is a term that specifically refers to the implicit conversion of
array types to pointer types.
Decay
is a term that specifically refers to the implicit conversion of
array types to pointer types.
The only time this decay doesn’t happen is when an array-typed variable is the operand to the address-of operator &, or when it is the operand of the the sizeof() operator.