Array of C languages

Source: Internet
Author: User

First: Array initial experience

We often use containers in our lives, such as when we go to the supermarket to buy goods in a shopping bag. Also we need the container in the program, but the container is a little special, it is in the program is a contiguous, fixed-size, data-type consistent memory space and it has a nice name. Array . The array can be understood as a fixed size, the items placed in a similar shopping bag, the items in the shopping bag are placed in a certain order.

Let's take a look at how to declare an array:

Data type array name [length];

Arrays can only be declared or not, see how arrays are initialized. When it comes to initialization, there are three types of array initialization in the C language, namely:

1. data type array name [length n] = {element 1, Element 2 ... Element n};

2. data type array name [] = {element 1, Element 2 ... Element n};

3 . Data type array name [length n]; array name [0] = element 1; array name [1] = element 2; array name [n] = element n+1;

How do we get the elements in the array after we put the data in the array?

When getting an array element: array name [element corresponding subscript];

such as: Initialize an array of int arr[3] = {------); Then arr[0] is element 1.

Attention:

1, the subscript of the array starts with 0;

2, when the array is initialized, the number of elements in the array cannot be greater than the declared array length;

3, if the first method of initialization, the number of elements is less than the length of the array, the extra array elements are initialized to 0;

4, when the array is declared without initialization, static (static) and external (extern) type array element initialization element is 0, automatic (auto) type of array element initialization value is indeterminate.



Second: The traversal of the array

When we went back to the supermarket and bought a bunch of things, I thought it would be perfect if there was a robot that could help me put things out.

So in the program, the array can iterate through each element in a loop, instead of having to specify the elements at a certain location for each acquisition, for example, we iterate over an array with a For loop:


The following points should be noted when iterating over arrays:

1, it is best to avoid the array of cross-border access, the loop variable should not exceed the length of the array, such as:

2, the C language array length once declared, the length is fixed, cannot change, and C language does not provide a method to calculate the length of the array.

Since c is a mechanism that does not check array length changes or array out-of-bounds, it may be compiled and passed in the editor, but the result is not certain, so do not cross the border or change the length of the array.


Three: array as function parameter

As we've learned before, variables can be used as parameters, right? Here the array can also be used as a function of the parameter drop, ah? What the? You ask the array how to do parameters? Take a look at the following knowledge.

An array can be used by an entire array as an argument to a function, or as an argument to a function by an element in the array:

1, the entire array as a function parameter, that is, the array name passed into the function, for example:

2. The elements in the array are treated as function parameters, that is, the parameters in the array are passed into the function, for example:

The following items are noted when an array is a function parameter:

1. When the array name is passed as a function argument, the array type parameter at the function definition as the receive parameter can either specify the length or not specify the length.

2. When an array element is passed as a function argument, the element type of the array must be the same as the shape parameter data type .


Four: Array application one--bubble sort

There are a lot of methods of sorting, here is a small series to introduce a more classic and relatively easy to master the sorting method: Bubble Sort .



Five: Strings and arrays

In the C language, there is no way to directly define string data types, but we can use arrays to define the strings we want. The following two formats are generally available:

1,char string name [Length] = "String value";

2,char string name [length] = {' character 1 ', ' character 2 ',..., ' character n ', ' + '};

Attention:

1, [] The length of the can be omitted to write;

2, in the 2nd way when the last element must be ' s ', ' \ ' to indicate the end of the string symbol;

3, in the 2nd way when the array can not write Chinese .

To use when outputting a string:printf ("%s", character array name); or puts (character array name);. For example:

Run the result as


Six: String Functions

The usual string functions are as follows:

Use the string function to note the following:

1, strlen () Gets the length of the string, which is not included in the string length, and the length of the kanji and letters is not the same. Like what:

2, strcmp () in comparison, the string will be converted to ASCII code before comparison, the result of 0 means that the ASCII code of S1 and S2 is equal , the return result of 1 means that S1 is larger than the ASCII code S2 , A return result of 1 indicates that S1 is smaller than the ASCII code of S2 , for example:

3. strcpy () copy will overwrite the original string and cannot copy the string constants, for example:

4, strcat in use S1 and S2 refers to the memory space can not overlap, and s1 to have enough space to accommodate the string to be copied, such as:


Seven: Multidimensional arrays

The definition format for multidimensional arrays is:

Data type array name [constant-expression 1][constant expression 2] ... [constant expression n];

For example, this defines a two-dimensional array called Num, which has a data type of int . The first [3] represents the length of the subscript in the first dimension, just like shopping when shopping is sorted, and the second [3] represents the length of the second-dimension subscript, just like the elements in each shopping bag.

We can think of the above array as a 3x3 matrix, such as:

The initialization of multidimensional arrays is similar to the initialization of one-dimensional arrays in two ways:

1. data type array name [constant expression 1][constant expression 2] ... [constant expression N] = {{value 1,.., value n},{value 1,.., value n},..., {value 1,.., value n}};

2. data type array name [constant expression 1][constant expression 2] ... [constant expression n]; Array name [subscript 1][subscript 2] ... [Subscript n] = value;

The following considerations are important for multidimensional array initialization:

1. When the first type of initialization is used, the array declaration must specify the number of dimensions of the column. because the system allocates space according to the total number of elements in the array, when the total number of elements and the dimensions of the columns are known, the dimension of travel is calculated directly ;

2. When the second type of initialization is used, the array declaration must specify both the row and the dimension of the column.



Array of C languages

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.