Linux shell array variables, types, and rules

Source: Internet
Author: User

There are two ways to assign values to an array variable:

(1) name = (value1... valuen) The subscript starts from 0.

(2) name [index] = value

Arrays are a form of organizing several variables of the same type in order to facilitate processing in programming. The collections of these sort-by-order data elements are called arrays. In C, arrays are constructed data types. An array can be divided into multiple array elements, which can be basic data type or constructor type. Therefore, Arrays can be divided into numeric arrays, character arrays, pointer arrays, Structured arrays, and other categories based on the types of array elements.

Array type

1. the array type is actually the value type of the index group element. For the same array, the Data Types of all its elements are the same.

2. The writing rules of array names should comply with the writing rules of identifiers.

3. the array name cannot be the same as other variable names.

Related rules

1. Only initial values can be assigned to some elements. When the number of {} values is less than the number of elements, only the previous element is assigned a value. For example, static int a [10] = {0, 1, 2, 3, 4}; indicates that only a [0] ~ A [4] assigns values to five elements, and the last five elements are automatically assigned 0 values.

2. You can only assign values to elements one by one, but not to the entire array. For example, if one value is assigned to all ten elements, the value can only be set to static int a [10] = {,}, but cannot be set: static int a [10] = 1; (Note: this is true in C, but not in all places involving arrays)

3. If no initial value is assigned to the initialized array, all elements are 0.

4. If you assign values to all elements, you can leave the number of array elements in the array description. For example: static int a [5] = {1, 2, 3, 4, 5}; can be written as: static int a [] = {1, 2, 3, 4, 5 }; you can assign values to arrays dynamically during program execution. In this case, the circular statements can be used with the scanf function to assign values to array elements one by one.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.