Definition and use of Arrays

Source: Internet
Author: User
Tags array definition

I. 1. Definition of one-dimensional array:

Type specifier array name [constant expression]; Note: constant expressions include constants and symbol constants, and cannot contain variables.

2. Reference of a one-dimensional array:

Array name [subscript];

3. initialize a one-dimensional array:

A. assign an initial value to the array element when defining the array;

B. assign values to only some elements;

C. To set the value of all elements in an array to 0, write it as a [10] = {0 };

Note that int A [10] = {1}; the array cannot be initialized to full 1, but the first is initialized to 1, followed by all 0 drops.

D. When assigning initial values to all array elements, you can leave the array length unspecified.

Ii. 1. Two-dimensional array definition:

Type specifier array name [constant expression] [constant expression];

2. 2d array reference:

Array name [subscript] [subscript];

3. Two-dimensional array initialization:

A. The Branch assigns a value to the two-dimensional array. Int A [3] [4] = {1, 2, 3, 4}, {5, 6, 7, 8}, {, 9, 10, 11, 12 }};

B. You can write all the data in a curly arc and assign the initial values to each element in the order of arrays.

C. You can assign initial values to some elements:

D. If an initial value is assigned to all elements (that is, all the initial data is provided), the length of the first dimension can be unspecified when the array is defined, but the length of the second dimension cannot be saved.

3. 1. Definition of character array: Refer to definition of one-dimensional array, type change.

2. character array initialization: refer to one-dimensional array initialization.

3. References to character Arrays: see references to one-dimensional arrays.

4. String and string end sign: the string ends when '\ 0' is encountered.

5. input and output of the character array:

A. Character-by-character input and output, using "% C ";

B. input and output the entire string once with "% s ".

Note:. the output character does not include the terminator '\ 0'; B. when "% s" is used to output a string, the input item in the printf function is the character array name rather than the array element name. C. If the array length is greater than the actual length of the string, it is output only to '\ 0. D. If a character array contains more than one '\ 0', the output ends when the first' \ 0' is used.

6. string processing functions:

A. Puts (character array): outputs a string (Character Sequence ending with '\ 0') to the terminal.

B. Gets (character array)Purpose: enter a string to the character array at the terminal and obtain a function value. The function value is the starting address of the character array.

Note: The putshe gets function can only input or output one string and cannot be written as puts (str1, str2) or gets (str1, str2 ).

C. strcat (character array 1, character array 2)Purpose: connect the strings in the two character arrays, connect string 2 to the end of string 1, and put the result in character array 1. After the function is called, a function value is obtained, that is, the address of character array 1.

Note: A. character array 1 must be large enough to accommodate the new string after connection. B. there is a '\ 0' behind the strings connected to the cash grain. During the connection, the' \ 0' behind string 1 is canceled, and only one '\ 0' is reserved at the end of the new string '.

D. strcpy (character array 1, string 2)Purpose: Copy string 2 to character array 1.

Note: A. character array 1 must be defined to be large enough to accommodate the copied string. The length of character array 1 should not be less than the length of string 2. B. "character array 1" must be written as an array name. "string 2" can be a character array name or a String constant. C. Copy the string to array 1 together with '\ 0. D. A String constant or character array cannot be directly assigned to a character array using the value assignment statement. E. You can use the strcpy function to copy the first several characters in string 2 to character array 1. For example, strcpy (str1, str2, 2 );

E. strcmp (string 1, string 2)Purpose: Compare string 1 and string 2.

Description: A. If String 1 = string 2, the function value is 0. B. If String 1> string 2, the function value is a positive integer. C. If String 1 <string 2, the function value must be negative. Note: To compare two strings, you can only use strcmp (string 1, string 2), rather than other forms.

F. strlen (character array)Purpose: test the string length function. The value of the function is the actual length of the string, excluding '\ 0.

G. strlwr (string)Purpose: replace uppercase and lowercase letters with lowercase letters.

H. strupr (string)Purpose: replace uppercase and lowercase letters with uppercase letters.

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.