Accessing array elements
Accessing array elements in C language is a simple process, and in this, square brackets [ ] are used after the array name. Which contains the index memory address location of that declared array element. Which you want to access or process in the current program.
So let’s access array elements in C program.
Accessing array elements in C.
So let’s access array elements in C program.
Accessing array elements in C.
So let’s declare an array with the name arrvalue in the C program. So you can access or process an array elements using the following program syntax.
arrvalue[index]
arrvalue – is the name of the declared array in the C program.
index – is an integer expression declared in the C program array. Which specifies the memory storage location of that array element, which you can access in the C program compilation. Always remember that the indexing location of any array in the C program starts from 0.
Array index location example in C.
So let’s access the element location of an integer array with 4 elements in a C program.
int decimal[4] = {1, 2, 3, 4};
So let’s access the individual elements of this array.
printf(“%d\n”, decimal[0]); // References first array element (index 0) address, output – 1
printf(“%d\n”, decimal[2]); // References third element (index 2) address, output – 3
Some important points to keep in mind for arrays.
Zero-based array indexing – Array index location in C program always starts from 0. So, first element of array arrname can be accessed using arrname[0] index location, and second array element can be accessed and processed using arrname[1], and similarly other array memory address locations are referenced.
Valid index range – Accessing array indexes must be within the bounds of the array. For an array arrname of array size n, valid array index locations range from 0 to n-1. Accessing an array element outside this array element location range results in a wrong array access location error, which can potentially lead to array program runtime errors or incorrect array data access.
Using variables as indexes – You can use variables or expressions in array programs that evaluate to integers as array indices.
Example:
int index = 4;
printf(“%d\n”, value[index]); // Accesses the element at index 4 in the declared array.
Output – 4
Modifying array elements – Array elements in a C program can be modified directly through their index locations.
decimal[4] = 9; // Modifies the fourth element of ‘array numbers’ by 9
Multidimensional arrays – Multi-dimensional array (array of arrays) in C programs For arrays in matrix or table format, accessing array elements involves using the index location for each array dimension.
int table[3][3] = {
{0, 1, 2},
{3, 4, 5},
{6, 7, 8}
};
printf(“%d\n”, table[1][2]); // Accesses the array element at row 1, column 2; Output – 5
Using array elements in program loops.
Arrays are commonly used in C program loops to process each element in sequence order or based on specific conditions. For example, iterating through a C program array can be done using a for loop.
for (int p = 0; p < 7; p++) {
printf(“%d “, decimal[p]); // Prints each element of the ‘decimal’ array
}
printf(“\n”);
Here in the for loop array, p iterates over the array index location from 0 to 7, and the loop C program provides access to each element location of the decimal array.