[C Language] 08-array

Source: Internet
Author: User

Note: This C language topic is a prelude to iOS development. To enable programmers with object-oriented language development experience to quickly get started with the C language. If you have no programming experience or are not interested in C and iOS development, ignore

To help you better learn and understand arrays, let's first get to know the "Address" in the memory ".

Address

1. Memory in the computer is the unit of storage space in bytes. Each byte in the memory has a unique ID, which is called an address. Every program and data stored in the memory has an address, that is, a function also has its own memory address.

 

2. When a variable is defined, the system allocates a storage unit with a unique address to store the variable. For example:

Char a = 'a'; // the ASCII value of A is 65int B = 66;
In the 16-bit compiler environment, the system allocates 1-byte and 2-byte storage units for a and B respectively. The address of the first byte of the variable storage unit is the address of the variable. It can be seen that the address of variable a is ffc3; the address of variable B is ffc1. The memory stores binary data. 3. During the debugging process, we can print the address of the variable:

Int c = 10; // output address printf in hexadecimal format ("hexadecimal: % x \ n", & c ); // output address printf ("10 hexadecimal: % d", & c );

Output result:

 

1. One-dimensional array 1. Definition of one-dimensional array

* The format is as follows:Type array name [number of elements]

int a[5];

 

* [] Can only be placed behind the array name. The following are incorrect statements:

Int [5] a; // error int [] B; // Error

 

* [] The number must be a fixed value, which can be a constant (for example, 6 or 8) or a constant expression (for example, 3 + 4 or 5*7 ). You cannot use variables or variable expressions to represent the number of elements. In most cases, do not omit the number of elements (except when an array is used as a function parameter and an array is initialized)

The following statements are correct:

Int a [5]; // integer constant int B ['a']; // character constant, which is actually A 65int c [3*4]; // integer constant expression

The following are all incorrect statements:

Int a []; // The number of elements is not specified. The error int I = 9; int a [I]; // The number of elements using variables is incorrect.

 

2. Storage of one-dimensional arrays

When an array is defined, the system allocates a continuous storage space based on the array type and quantity to store array elements, for example, int a [3] occupies 6 consecutive bytes of storage space (in a 16-bit compiler environment, an int type occupies 2 bytes ). Note that the array name represents the address of the entire array, that is, the starting address of the array.

 

Note: In fact, a is not a variable, but a constant, which represents the address of the array. Put a in the variable column to help you understand the array structure.

The address of array a is ffc1, the address of a [0] Is ffc1, the address of a [1] is ffc3, and the address of a [2] is ffc5. Therefore, the address of the first element is the address of the entire array.

 

3. initialize a one-dimensional array

* The General initialization form is: type array name [number of elements] = {element 1, element 2 ,...};

int a[2] = {8, 10};

Actually equivalent:

int a[2];a[0] = 8;a[1] = 10;
Note: in C language, the compiler does not check the array subscript out of bounds. Therefore, be careful when accessing array elements. * The element value list can be the initial values of all elements of the array, it can also be the initial values of the preceding elements.
int a[4] = {2, 5};

When the array is an integer, the initialization of uncertain initial value elements, the default value is 0, so the above a [2], a [3] are 0

 

* When an initial value is assigned to all array elements, the number of elements can be omitted.

int a[] = {2, 5, 7};

The number of elements in array a is 3.

 

* The array initialization method can only be used for the definition of an array. After the definition, only one element can be assigned a value to one element.

The following statement is incorrect:

1 int a [3]; 2 a [3] = {1, 2, 3}; // error 3 a = {1, 2, 3}; // Error

In fact, why is it wrong? A brief analysis is provided.

1> Row a [3] of Row 3 represents the access to the 2nd elements of the array. First, the array subscript is out of bounds. Even if it is not out of bounds, when assigning a value to a [3], you should also assign an integer of the int type, not {}.

2> row a of Row 3 is the array name, representing the address of the array. It is a constant! Assign a value to a constant. It must be wrong!

 

4. One-dimensional array and function parameters

If you forget the meaning of the real parameter and the form parameter, you can go back to the Article 4. functions.

* The elements of a one-dimensional array are used as function arguments. Like simple variables of the same type as real parameters, they are unidirectional value transfer. That is, the values of the array elements are transmitted to the form parameters. Changes in the form parameters do not affect the real parameters.

// B is the form parameter of the test function (form parameter) void test (int B) {B = 9;} int main () {int a [3]; a [0] = 10; printf ("a [0]: % d \ n", a [0]); test (a [0]); // a [0] is the real parameter of the test function (actual parameter) printf ("a [0]: % d", a [0]); return 0 ;}

Output result:

 

* As we all know, the array name represents the address of the entire array. If the name of a one-dimensional array is used as the real parameter of the function, the whole array is passed, that is, the form parameter array and the real parameter group are exactly the same, is the same Array stored in the same bucket. In this way, the real parameter array is modified at the same time. The number of elements in the parameter array can be omitted.

// B is the form parameter of the test function (form parameter) void test (int B []) {// You can also write int B [3] B [0] = 9 ;} int main () {int a [3]; a [0] = 10; printf ("a [0] Before function call: % d \ n ", a [0]); test (a); // a is the real parameter of the test function (actual parameter) printf ("a [0]: % d after function call ", a [0]); return 0 ;}

Output result:

 

2. Two-dimensional array 1. Definition of two-dimensional array

Definition form:Type array name [number of rows] [number of columns]

Int a [2] [3]; // two rows, three columns, and six elements in total

 

2. Storage of two-dimensional arrays

* The C language regards a two-dimensional array as a collection of One-dimensional arrays, that is, a two-dimensional array is a special one-dimensional array. Its element is a one-dimensional array. For example, int a [2] [3] can be considered to be composed of one-dimensional array a [0] and one-dimensional array a [1]. Both one-dimensional arrays contain three int-type elements.

 

* The order in which two-dimensional arrays are stored is Row-based. First, the elements of the first row are stored, and then the elements of the second row are stored. For example, the storage order of int a [2] [3] is: a [0] [0] → a [0] [1] → a [0] [2] → a [1] [0] → a [1] [1] → a [1] [2]

 

* Let's take a look at the memory storage, for example, int a [2] [2].

(Note: a [0] And a [1] are also arrays, which are one-dimensional arrays, and a [0] And a [1] are array names, therefore, a [0] And a [1] represent the addresses of this one-dimensional array)

1> the address of array a is ffc1, and the address of array a [0] Is ffc1, that is, a = a [0];

2> the address of element a [0] [0] Is ffc1, so the address of array a [0] is the same as that of element a [0] [0, that is, a [0] = & a [0] [0];

3> the final conclusion is as follows: a = a [0] = & a [0] [0], and so on. a [1] = & a [1] [0] can be obtained.

 

3. Two-dimensional array Initialization

* Initialize by row

int a[2][3] = { {2, 2, 3}, {3, 4, 5} };

 

* Initialize in the storage Order (store 1st rows first and then 2nd rows)

int a[2][3] = {2, 2, 3, 3, 4, 5};

 

* Initialize some elements.

int a[2][3] = { {2}, {3, 4} };int b[3][3] = { { }, { , , 2}, {1, 2, 3}};

 

* If only some elements are initialized, the number of rows can be omitted, but the number of Columns cannot be omitted.

int a[][3] = {1, 2, 3, 4, 5, 6};int a[][3] = {{1, 2, 3}, {3, 5}, {}};

Some may wonder why the number of rows can be omitted, but not the number of columns. Someone may ask, can I only specify the number of rows, but omit the number of columns?

In fact, this problem is very simple. If we write:

Int a [2] [] = {1, 2, 3, 4, 5, 6}; // incorrect syntax

As we all know, a two-dimensional array stores 1st rows of elements first. Because you are not sure about the number of columns, that is, you are not sure how many elements are stored in the 1st rows, there are many situations, maybe 1 and 2 belong to 1st rows, or 1, 2, 3, and 4 are the first rows, even rows 1, 2, 3, 4, 5, and 6 belong to 1st rows.

 

3D or even more multi-dimensional arrays are no longer mentioned, and so on.

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.