Definition and use of arrays in C language

Source: Internet
Author: User
Tags array definition strcmp
the definition and use of arrays

One, 1. Definition of one-dimensional array:

Type descriptor array name [constant expression]; Note: Constant expressions include constant and symbolic constants and cannot contain variables.

2. Reference to one-dimensional arrays:

Array name [subscript];

3. Initialization of one-dimensional arrays:

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

B. You can assign values to only a subset of the elements;

C. To make the value of all elements in an array 0, it can be written as: a[10]={0};

Need attention int a[10] = {1}; The array cannot be initialized to all 1, but the first is initialized to 1, followed by all 0 drops.

D. When you assign an initial value to all array elements, you can not specify the length of the array.

Definitions of two, 1. Two-D arrays:

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

2. Reference to two-D arrays:

Array name [subscript] [subscript];

3. Initialization of two-D arrays:

A. Branch assigns a value to a 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 bracket, assigning an initial value to each element in the order in which they are arranged.

C. You can assign an initial value to a partial element:

D. If you assign an initial value to all of the elements (that is, provide all the initial data), the length of the first dimension may not be specified when the array is defined, but the length of the second dimension cannot be saved.

Three, 1. Character array definition: Refer to one-dimensional array definition, type change.

2. Initialization of character arrays: Refer to one-dimensional array initialization.

3. Reference to a character array: reference to a one-dimensional array.

4. String and string End flag: string is encountered with ' "", indicating end of string.

5. Input and output of character array:

A. Character input and output, using "%c";

B. The entire string is entered and output at once, with "%s".

Note: A. The output character does not include The Terminator ' "; B. When you output a string with '%s ', the entry in the printf function is a character array name, not an array element name. C. If the length of the array is greater than the actual length of the string, it is output only to the '. ' End. D. If an array of characters contains more than one ', ' the output ends when the first ' is '.

6. String handler function:

A.puts (character array) function: Output a string (a sequence of characters ending with ', ') to the terminal.

B.gets (character array) function: Enter a string into a character array with a terminal, and get a function value. The function value is the starting address of the character array.

Note: The putshe gets function can only enter or output a string and cannot be written as puts (STR1,STR2) or gets (STR1,STR2).

C.strcat (character array 1, character array 2) function: Concatenate a string in a two-character array, string 2 to the back of the string 1, and the result is placed in the character array 1, and the function call gets a function value-the address of the character array 1.

Description: A. Character array 1 must be large enough to hold the new string after the connection. B. Connection Qianliang A string is followed by a ' yes ', which cancels the ' 1 ' of the string after the connection, leaving only a ' yes ' at the end of the new string.

d.strcpy (character array 1, string 2) Action: is to copy the string 2 to the character array 1.

Description: A. Character array 1 must be defined sufficiently large to accommodate the copied string, and the character array 1 should not be less than the length of the string 2. B. " The character array 1 "must be written as an array name, and" String 2 "can be either a character array name or a string constant. C. Copy to character array 1, along with the ' words ' following the string. D. An assignment statement cannot be used to give a string constant or an array of characters directly to an array of characters. E. You can use the strcpy function to copy the preceding characters in string 2 to character array 1. such as: strcpy (str1,str2,2);

E.STRCMP (String 1, String 2) function: is to compare string 1 and string 2.

Description: A. If the string 1 = string 2, the function value is 0. B. If the string is 1> string 2, the function value is a positive integer. C. If the string is 1< string 2, the function value must be negative. Note: Comparisons to two strings can only take the form of strcmp (string 1, String 2) and cannot take other forms.

F.strlen (character array) Action: a function that tests the length of a string, the value of which is the actual length in the string, excluding ' the '.

G.STRLWR (String) Action: Converts uppercase letters in a string to lowercase letters.

H.STRUPR (String) Action: Converts a string into 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.