Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use arrays in C language

Shulou Source: shulou.com Published: 2022-06-02 05:05:17 10月03日 Update

It is believed that many inexperienced people have no idea about how to use arrays in C language. therefore, this paper summarizes the causes and solutions of the problems. Through this article, I hope you can solve this problem.

The basic concept of 1 array

Array: a collection of data elements of the same type, which is a construction data type in C language.

These elements are sequentially stored in a certain area of memory.

2 array definition syntax array type array name [array size]

Array size: determines the number of elements that can be stored in the array.

Array size cannot be a variable, it can only be a literal value or a literal expression.

The C language compiler requires that the memory size of the array be determined during compilation.

The array size must be a positive integer greater than 0.

3 initialization of one-dimensional array 3.1 all initialization int arr [5] = {1,2,3,4,5}; / / arr [0]: 1//arr [1]: 2//arr [2]: 3//arr [3]: 4//arr [4]: 5

Initialization list: a list of values enclosed in curly braces and used to initialize an array, separated by commas.

The initialization list can only be used when defining the array, and you can no longer use the initialization list to assign values to the array after the array definition is complete.

Full initialization requires that the number of elements in the initialization list must be the same as the array size.

3.2 some elements are assigned initial values int arr [5] = {1,2,3}; / / arr [0]: 1//arr [1]: 2//arr [2]: 3//arr [3]: 0//arr [4]: 0

In actual development, the method of assigning initial values to some elements is usually used to initialize array elements, such as: int arr [100] = {0}

3. Omit the initial value int arr [] = {1, 2, 3, 4, 5}

When defining an array, you can omit the array size in parentheses if the initialization list is followed by an initialization list, and the number of values in the initialization list is the expected array size.

4 example of using one-dimensional array 4.1 to find the maximum, minimum, average # include int main (void) {int arr [10]; int 1, sum, max, min; / / command line to read 10 integers for (iS0; I

Tags: Array character element string one-dimensional sort score language size two-dimensional initial value space minimum part length output maximum content maximum type Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Tech Info NVidia Xiaomi Shulou Technology