C Arrays
# C Arrays
The C language supports the **array** data structure, which can store a fixed-size sequential collection of elements of the same type. Arrays are used to store a collection of data, but they are often thought of as a collection of variables of the same type.
Instead of declaring individual variables, such as tutorial0, tutorial1, ..., tutorial99, you declare one array variable such as and use , , ..., to represent individual variables.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element.

A specific element in an array is accessed by an index. The first index value is 0.
The C language also allows us to use pointers to handle arrays, which makes array operations more flexible and efficient.
!(#)
## Declaring Arrays
To declare an array in C, you need to specify the type of elements and the number of elements, as follows:
type arrayName ;
This is called a single-dimensional array. **arraySize** must be an integer constant greater than zero, and **type** can be any valid C data type. For example, to declare a 10-element array called **balance** of type double, the declaration would be:
double balance;
Now _balance_ is a usable array that can hold 10 numbers of type double.
## Initializing Arrays
In C, you can initialize an array element by element, or use a single initialization statement as follows:
double balance = {1000.0, 2.0, 3.4, 7.0, 50.0};
The number of values between the braces { } cannot be greater than the number of elements specified in the square brackets during array declaration.
If you omit the size of the array, the size of the array will be the number of elements used in the initialization. So if:
double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};
You will create exactly the same array as the one created in the previous example. The following is an example of assigning a value to a specific element in an array:
balance = 50.0;
The above statement assigns the value 50.0 to the fifth element of the array. All arrays have 0 as the index of their first element, which is also called the base index, and the last index of an array is the total size of the array minus 1. Below is the graphical representation of the array discussed above:

The image below shows an array of length **10**, with the first element having an index value of **0** and the ninth element **** having an index value of **8**:
!(#)
## Accessing Array Elements
An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example:
double salary = balance;
The above statement will take the 10th element from the array and assign the value to the salary variable. The following example makes use of all the above-mentioned three concepts: declaring arrays, assigning values to arrays, and accessing arrays:
## Example
#includeint main(){int n; /* n is an array of 10 integers */int i,j; /* initialize array elements */for(i = 0; i<10; i++ ){n = i + 100; /* set element at location i to i + 100 */}/* output each array element's value */for(j = 0; j<10; j++ ){printf("Element[%d] = %dn", j, n); }return 0; }
When the above code is compiled and executed, it produces the following result:
Element = 100Element = 101Element = 102Element = 103Element = 104Element = 105Element = 106Element = 107Element = 108Element = 109
## Getting Array Length
The array length can be obtained using the **sizeof** operator, for example:
int numbers[] = {1, 2, 3, 4, 5};int length = sizeof(numbers) / sizeof(numbers);
## Example
#include
int main(){
int array[]={1, 2, 3, 4, 5};
int length =sizeof(array)/sizeof(array);
printf("Array length: %dn", length);
return 0;
}
Using a macro definition:
## Example
#include
#define LENGTH(array) (sizeof(array) / sizeof(array))
int main(){
int array[]={1, 2, 3, 4, 5};
int length = LENGTH(array);
printf("Array length: %dn", length);
return 0;
}
The output of the above examples is:
Array length: 5
## Array Name
In C language, the array name represents the address of the array, which is the address of the first element. When we declare and define an array, the array name represents the address of that array.
For example, in the following code:
int myArray = {10, 20, 30, 40, 50};
Here, myArray is the array name. It represents an array of integer type containing 5 elements. myArray also represents the address of the array, which is the address of the first element.
The array name itself is a constant pointer, meaning its value cannot be changed. Once determined, it cannot point elsewhere.
We can use the & operator to get the address of the array, as shown below:
int myArray = {10, 20, 30, 40, 50};int *ptr = &myArray; // or simply write: int *ptr = myArray;
In the above example, the pointer variable ptr is initialized to the address of myArray, which is the address of the first element of the array.
It is important to note that although the array name represents the address of the array, in most cases, the array name is automatically converted to a pointer to the first element of the array. This means we can directly use the array name for pointer arithmetic, such as when passing parameters to functions or traversing arrays:
## Example
void printArray(int arr[],int size){
for(int i =0; i < size; i++){
printf("%d ", arr);// array name arr is used as a pointer
}
}
int main(){
int myArray={10,20,30,40,50};
printArray(myArray,5);// pass array name to function
return 0;
}
In the above code, the printArray function accepts an integer array and the array size as parameters. We pass the myArray array name to the function, and inside the function, the arr array name can be used just like a pointer.
## Detailed Explanation of Arrays in C
In C, arrays are very important, and we need to learn more about the details of arrays. Below is a list of some important concepts related to arrays that C programmers must understand:
| Concept | Description |
| --- | --- |
| (#) | C supports multi-dimensional arrays. The simplest form of a multi-dimensional array is the two-dimensional array. |
| (#) | You can pass a pointer to an array by specifying the array name without an index. |
| (#) | C allows returning an array from a function. |
| (#) | You can generate a pointer to the first element of an array by specifying the array name without an index. |
| (#) | C supports static arrays and dynamic arrays. |
YouTip