C - Accessing Elements of a One-Dimensional Array

In C, you can access elements of a one-dimensional array using square brackets [] with the index of the element you want to access. The index represents the position of the element within the array, and array indexing in C starts at 0.

Here's the general syntax for accessing an element of a one-dimensional array:


array_name[index]
        
    

Here's an example of how to access elements of a one-dimensional integer array in C:


#include <stdio.h>

int main() {
    int myArray[5] = {10, 20, 30, 40, 50};

    // Accessing elements of the array and printing them
    printf("Element 0: %d\n", myArray[0]); // Accessing the first element (index 0)
    printf("Element 1: %d\n", myArray[1]); // Accessing the second element (index 1)
    printf("Element 2: %d\n", myArray[2]); // Accessing the third element (index 2)
    printf("Element 3: %d\n", myArray[3]); // Accessing the fourth element (index 3)
    printf("Element 4: %d\n", myArray[4]); // Accessing the fifth element (index 4)

    return 0;
}
        
    

In this example, we have an integer array myArray with 5 elements. We access each element using its index within square brackets and then print the value to the console. Remember that C uses zero-based indexing, so the first element is at index 0, the second at index 1, and so on.

Output of the Program:

The output of the program provided in the previous response, which accesses and prints the elements of the myArray one-dimensional integer array, will look like this:


Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50
        
    

Each line of output corresponds to the access and printing of an element of the array. The values printed are the elements stored in the array at their respective indices.