Arrays
Arrays allow grouping values under a single name, the data items in an array are referred to as elements.
Initializing an array (lists)
float sample_data[500] = {1,2,3,4,5}; → The capacity is 500 values but we declared only 5 so there will be 495 empty values available and these numbers (1 to 5) are assigned to the first three elements of the array.
Designated initializers
c99 added a feature called designated initiazlizers allowing us to pick and choose which elements are initialized.
We can assign array elements values in any order by enclosing an element number in a pair of brackets.
So here element number 2 of array simple_data is 500.5 and so on.
To initialize a range in array:
Two-dimensional arrays
The c language allows arrays of any dimensions to be defined two dimension arrays are the most common one and the most natural application of this is the case of a matrix.
Example:
Three-dimensional arrays
For storing x, y, z values.
Example:
Processing elements in a N dimensional array
Variable length arrays (VLA)
It allows you to specify the size of an array with a variable when creating an array.
Introduced in c99.
A VLA keeps the same size after creation.
Flexible array members
A feature introduced in the c99 standard of the c language.
When using a structure we can declare an array without a dimension and whose size is flexible in nature.
A flexible array members size is variable (can be changed at runtime).
A flexible array member is declared by specifying empty square brackets [].
A flexible array can be declared only as the last member if a struct.
Each struct can contain at most one flexible array member.
A flexible array cannot be the only member of a struct.
Any struct containing a flexible array member cannot be a member of another struct.
A struct with a flexible array member cannot be statically initialized, it must be allocated dynamically.
You cannot fix the size of the flexible array member at compile time.
Last updated