1. What is an array?
An array is a aggregate type, which lets us access multiple variables through a single identifier. To access the individual values the identifier and an index is required.
2. Which index would you use to access the 2nd variable in an array?
1, as the indexing starts to count at 0
3. What can you say about the size of an array?
The size of an array most commonly is its length. Furthermore the size can also be the array length multiplied by the element size, which gives the array size in byte.
For static arrays the length is defined at compile time for dynamic arrays the size is defined at runtime.
-
How can you initialize an array?
With an initializer list. The list can be empty, in this case all values are initialized with 0. Alternatively the values can be provided in the list. Are there fewer items in the list as the length of the array, these items will be initialized with 0. -
How can you get the size of an array in bytes?
With the sizeof() function. This will return the array size: array length * element size -
How can you use the function sizeof in order to calculate the number of elements in an array?
By dividing the byte size by the size of one element:
sizeof(array)/sizeof(array[0]) -
What happens when you try to write outside the bounds of the array?
The value will get stored at the memory location where this additional item would have been located. This can result in over writing values of another variable. It will cause undefined behavior, potentially crashing the program.