A one-dimensional array in C language is a collection of elements of the same data type that are stored in a contiguous block of memory. The elements of the array are accessed by their index, which is a positive integer that represents the position of the element in the array.
Here is an example of declaring a one-dimensional array of integers in C:
int array[10]; // declare an array of 10 integers
In this example, the array is declared with 10 elements, and each element is an integer. The elements of the array are stored in a contiguous block of memory, and can be accessed using an index that ranges from 0 to 9.
You can also initialize a one-dimensional array with values when it is declared:
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // declare and initialize an array of 10 integers
In this example, the array is initialized with the given values, in the order they appear. The size of the array is inferred from the number of values provided.
You can access the elements of a one-dimensional array using the array indexing operator []:
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // declare and initialize an array of 10 integers
int element = array[5]; // access the element at index 5
In this example, the element at index 5 is accessed and stored in the variable “element”. The value of “element” will be 5.
You can also use a loop to access the elements of a one-dimensional array:
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // declare and initialize an array of 10 integers
int i;
for (i = 0; i < 10; i++) {
printf("Element at index %d is %d\n", i, array[i]); // print the element at index i
}
In this example, the loop iterates from 0 to 9, and prints the elements of the array using the array indexing operator. The output will be:
Element at index 0 is 0
Element at index 1 is 1
Element at index 2 is 2
Element at index 3 is 3
Element at index 4 is 4
Element at index 5 is 5
Element at index 6 is 6
Element at index 7 is 7
Element at index 8 is 8
Element at index 9 is 9