Arrays
is a very common in a program to store many data values of a specific type. arrays allow grouping values under a single name. the data items in an array are referred to as elements
to call or assign values to each element:
example:
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)
introduced in c99. it allows you to specify the size of an array with a variable when creating an array. 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