C Language Basics: Simple Arrays

        In this section, we will learn about arrays together. In the first chapter, we have briefly learned to define and use arrays. In this chapter, we will learn the details of arrays, and combine with arrays to learn the concept and application of pointers.

       Let's first take a look at the definition of an array. Look at the following example. In fact, there is no difference between defining an array and defining an ordinary variable. When defining a variable, you just apply the variable to the system for a variable, and defining an array is equivalent to applying to the system. Multiple continuous variables are applied, for example:

short array[5] = { 5, 6, 7, 8, 9 };

        In this way, we have defined 5 continuous variables, the types of these variables are all short, and the positions of these variables in memory are continuous, such as the following figure:

        The values ​​of these variables are the values ​​5, 6, 7, 8, 9 that we defined in the curly brackets. The process of assigning an array with curly braces is called array initialization. Note that array initialization can only be used when the array is defined, not after it is defined. For example, the following approach is wrong:

short array[5];
array = { 5, 6, 7, 8, 9 };

        After initializing the array, we can use the variables in the array. When defining the array, the number in brackets after the array variable indicates the total size of the array. When using an array, you can use the array name [subscript] to use array variables. For example, in the above example we defined an array with 5 elements, then we can use them like this:

array[0] = 11;
array[1] = 12;
array[2] = 13;
// ...

         As you can see, we use the subscript of the array to use the element variable in the array. In fact, each array name [subscript] represents an array element. Note: In C, most compilers do not check the range of their subscripts. For example, in the above example we define an array with 5 elements, then the normal valid subscript range is 0 to 4 and similar to -2, -3, -1, 5, 6, 7, 8... Wait, when such a subscript is used in the array array, the compiler will not verify its legality. That is to say, the compiler will not prompt an error when compiling such code, but this is not reasonable, and we remind the reader to pay special attention to the subscript range of the array when using the array. Of course, when using array variables, the subscripts in square brackets can not only define values, but also expressions, variables and expressions, such as array[a+b] = 3;   array[a-1] = 5 ;   array[a] = array[a*2]; and so on.

      For example, use a loop to display all the values ​​in an array:

for (int i = 0; i < 5; i++)
{
    printf("%d\n", array[i]);
}

        Let's look at array definitions for other types

char array1[5] = {'H','e','l','l','o'};
int array2[5] = {1,2,3,4,5};
long array3[5];
float array4[5];

        The definition, initialization and use of arrays of character type are the same as that of other types of arrays. But for character arrays, they can be used as strings. We will learn about the definition and use of character arrays and strings in the next section.


Welcome to the public account: programming aliens

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325691222&siteId=291194637