Declaration and initialization

Declaration and initialization

An array is a homogeneous data structure in C programming that allows the C programmer to index or store a fixed-size sequence of elements of the same data type. Arrays in C language provide a way to organize integer, float, character, or string data types that can be accessed and manipulated using the index method in C language.

Declaration and initialization

Some main features of arrays in C language.

Fixed size – Once an array is declared in a C program, the default size of the declared array remains constant throughout its programming lifetime. This array size is determined at program compilation, and must be declared using a data type constant expression.

Indexed access – Always remember that in C language, elements within an array are accessed and processed in the program using integer index locations starting from 0. For example, in an array arrname, arrname[0] refers to the first array element address, arrname[1] refers to the second array element address in the same declared array, and similarly, arrname[2] refers to the third array element address in the array.

Homogeneous array elements – Always remember that in C language all the elements in the declared array can be of the same data type (e.g., int, float, char, double, long double etc.) data nature.

Contiguous array memory allocation – In C language program all the elements of declared array are stored in the memory in sequence order. Where C language provides efficient access to array element memory address location using pointer method arithmetic.

Array Declaration and Initialization.

Array declaration syntax in C language.

arraytype arrayName[arraysize];

arraytype – Specifies the data type of array elements in the array declared in C program. Such as, (int, float, char, double,string,long double) etc.

arrayName – An identifier used to refer to the variable name of the array declared in C program.

arraysize – Specifies the number of array elements in C language. Which should be a constant static array expression.

Array declared in C program can be declared or initialized either statically (at program compilation time) or dynamically in the program (at runtime).

Array static initialization – At the time of array declaration in C language program, data type variable is initialized with a predefined set of values.

int decimal [6] = {0, 2, 3, 8, 9, 7};

Dynamic array initialization – In C programs, array elements can be initialized during runtime with a program loop or input function.

int decimal[6];

for (int p = 0; p < 6; p++) {

scanf(“%d”\n, &decimal[p]);

}

Accessing array elements.

In C programs, array is declared in square brackets [ ], and each array element is accessed using the zero array index location.

int num[5] = {7, 8, 2, 6, 9};

printf(“%d\n”, arr[0]); // Array 0 address element address output – 7

printf(“%d\n”, arr[2]); // Array 2 address element address output – 2

printf(“%d\n”, arr[4]); // Array 4 Address Element Address Output – 9

Example of Multidimensional Array in C Language.

Arrays in C language can be of multiple dimensions. Such as, one dimensional array, two dimensional array, three dimensional array, or multi dimensional array. For example, a 2D array in a C program can be declared and accessed as follows.

int table[3][3] = {

{11, 17, 15},

{22, 55, 78},

{99, 55, 11}

};

printf(“%d\n”,table[1][2]);

Advantages and Limitations of Arrays.

Array Advantages – Array elements provide efficient random access in C language` programs, and are useful for storing and processing arrays of similar types of data.

Array Limits – The size of an array declared in a C program is a fixed size, which is determined by the programmer at program compile time, making it flexible in programs when the number of array elements varies or needs to be determined dynamically.