Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

C array


May 11, 2021 C


Table of contents


C array

The C language supports the array data structure, which can store a sequential collection of elements of the same type at a fixed size. An array is used to store a series of data, but it is often considered a series of variables of the same type.

The declaration of an array is not to declare a single variable, such as number0, number1、...、number99, but rather to declare an array variable, such as numbers, and then to represent a separate variable using numbers 、...、 . Specific elements in an array can be accessed through an index.

All arrays consist of continuous memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element.

C array




Declare the array

To declare an array in C, you need to specify the type of element and the number of elements, as follows:

type arrayName [ arraySize ];

This is called a one-dimensional array. must be an integer constant greater than zero, type can be any valid C data type. For example, to declare an array of 10 elements of double type double, declare the following statement:

double balance[10];

Balance is now an available array that can hold 10 numbers of double type.

Initialize the array

In C, you can initialize the array one by one, or you can use an initialization statement, as follows:

double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

The number of values between braces cannot be greater than the number of elements we specify in square brackets when declaring an array.

If you omit the size of the array, the size of the array is the number of elements at the time of initialization. Therefore, if:

double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};

You will create an array that is exactly the same as the one created in the previous instance. Here's an example of assigning a value to an element in an array:

balance[4] = 50.0;

The statement above assigns the value of the fifth element in the array to 50.0. A ll arrays are indexed with 0 as their first element, also known as the base index, and the last index of the array is the total size of the array minus 1. The following is a graphical view of the array discussed above:

C array

Access array elements

Array elements can be accessed by indexing the array name. T he index of the element is placed in square brackets, behind the array name. For example:

double salary = balance[9];

The statement above assigns the value of the 10th element in the array to the salary variable. The following example uses the three concepts above, namely, declaring arrays, array assignments, accessing arrays:

#include <stdio.h>
 
int main ()
{
   int n[ 10 ]; /* n 是一个包含 10 个整数的数组 */
   int i,j;
 
    /* 初始化数组元素 */         
    for ( i = 0; i < 10; i++ )    {

        n[ i ] = i + 100; /* 设置元素 i 为 i + 100 */
    }        /* 输出数组中每个元素的值 */
    for (j = 0; j < 10; j++ )    {
        printf("Element[%d] = %d\n", j, n[j] );
    }
    return 0;
} 

When the above code is compiled and executed, it produces the following results:

Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109

The array in C is explained in detail

In C, arrays are very important and we need to know more about arrays. Here are some important concepts related to arrays that C programmers must be aware of:

concept describe
Multidimensional Arrays C supports multidimensional arrays.The simplest form of multidimensional arrays is a two-dimensional array.
Pass array to function You can deliver a pointer to the array by specifying an array name without an index.
Returns an array from the function C Allows the array from a function.
Pointer to array You can generate a pointer to the first element in an array by specifying an array name without an index.