C language 08-arrays

Source: Internet
Author: User

directory of this document
    • Address
    • One or one-D arrays
    • Two or two-D arrays

Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experience, or are not interested in C or iOS development, please ignore.

In order to get everyone to learn and understand the array better, let's first meet the "address" in memory.

Address

1. The memory in the computer is the storage space in bytes. Each byte of memory has a unique number, which is called an address. Every program and data stored in memory has an address, that is, a function 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. Like what:

char a = ' a '; The ASCII value of a is 65int B = 66;
In the 16bit compiler environment, the system allocates 1 bytes, 2 bytes of storage unit for a and B respectively. The address of the first byte of a variable storage unit is the address of the variable. As you can see, the address of variable A is FFC3, and the address of variable B is ffc1. 2 binary data is stored in memory. 3. During the commissioning process, we take a print view of the variable's address:
int c = 10;//output address printf in 16 binary form ("16 binary:%x\n", &c);//output address printf in 10 binary form ("10 Binary:%d", &c);

Output Result:

One or one-D arrays

1. Definition of one-dimensional arrays

* The form of the definition is: type array name [number of elements]

int a[5];

* [] can only be placed after the name of the array, the following are the wrong wording:

Int[5] A; Error int[] b; Error

* [] The number must be a fixed value, can be a constant (such as 6, 8), a constant expression (such as 3+4, 5*7). You must never use a variable or variable expression to represent the number of elements, and in most cases do not omit the number of elements (except when the array is initialized as a function's formal parameters and arrays)

The following are the correct wording:

int  a[5];   INTEGER constant int  b[' A '];  Character constant, is actually 65int  c[3*4];  Integer constant expression

The following are the wrong wording:

int a[]; No number of elements specified, error int i = 9;int A[i]; Use variables to make the number of elements, error

2. Storage of one-dimensional arrays

When you define an array, the system allocates a contiguous amount of storage space by array type and number to store the array elements, such as int a[3], which occupies a contiguous 6 bytes of storage (in a 16-bit compiler environment, an int type occupies 2 bytes). Note that the array name represents the address of the entire array, which is the starting address of the array.

Note: A is not a variable, but a constant, which represents the address of an array. Put a in the variable column to facilitate understanding of the array structure.

The address of the array A is ffc1,a[0] is the address of ffc1,a[1] and the address of ffc3,a[2] is ffc5. So a = = &a[0], that is, the address of the first element is the address of the entire array.

3. Initialization of one-dimensional arrays

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

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

is actually equivalent to:

int a[2];a[0] = 8;a[1] = 10;
Note that the compiler in the C language does not check for an array subscript out of bounds, so be careful when accessing the elements of an element: the list of elements values can be the initial value of all elements of an array, or it can be the initial value of an element in the previous section
int a[4] = {2, 5};

When an array is an integer, initialize an element with an indeterminate initial value, which defaults to 0, so the above a[2], a[3] are 0

* When assigning initial values to all array elements, you can omit the number of elements

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

Indicates that the number of elements in array A is 3

* Array initialization can only be used for the definition of the array, after the definition can only one element of an element to assign a value

The following syntax is incorrect:

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

In fact, why is the wrong wording? We can briefly analyze it.

1> the 2nd line of the a[3] represents the 4th element of the Access array, first here is already an array subscript out of bounds, even if there is no bounds, assign a value to A[3] should also be assigned an int type of integer, should not be {}.

2> the 3rd row of a is the array name, representing the address of the group, it is a constant! Assign a value to a constant, it must be wrong!

4. One-dimensional arrays and function parameters

If you forget the meaning of the argument and the formal parameter, you can look at the previous article.

* Elements of a one-dimensional array as function arguments, like simple variables of the same type as arguments, are one-way value passing, that is, the value of the array element is passed to the parameter, and the change of the parameter does not affect the argument

B is the parameter of the test function (formal parameter) void test (int b) {    b = 9;} int main () {    int a[3];    A[0] = ten;        printf ("a[0]:%d\n before function call", a[0]);        Test (a[0]); A[0] is the argument (actual argument) of the test function    printf ("a[0]:%d after function call", a[0]);    return 0;}

Output Result:

* As you know, the array name represents the address of the entire array, and if the name of one-dimensional array is used as a function argument, the entire array is passed, that is, the shape parameter group and the real parameter group are identical and are the same arrays that are stored in the same storage space. When this parameter group is modified, the real parameter group is also modified. The number of elements in the shape parameter group can be omitted.

B is the parameter of the test function (formal parameter) void Test (int b[]) {//can also write int b[3]    b[0] = 9;} int main () {    int a[3];    A[0] = ten;        printf ("a[0]:%d\n before function call", a[0]);        Test (a); A is the argument of the test function (the actual argument)    printf ("a[0]:%d after function call", a[0]);    return 0;}

Output Result:

Two or two-D arrays

1. Definition of two-D arrays

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

int a[2][3]; Total 2 rows 3 columns, 6 elements

2. Storage of two-D arrays

* The C language treats two-dimensional arrays as a set of one-dimensional arrays, that is, a two-dimensional array is a special one-dimensional array: its elements are one-dimensional arrays. For example, int a[2][3] can be thought of as consisting of a one-dimensional array a[0] and a one-dimensional array a[1], the two one-dimensional arrays contain 3 elements of type int.

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

* Take a look at storage in memory, such as int a[2][2]

(Note: a[0], a[1] is also an array, is a one-dimensional array, and a[0], a[1] is the name of the array, so a[0], a[1] represents the address of the one-dimensional array)

The address of the 1> array A is FFC1, and the address of the array a[0] is also ffc1, i.e. a = a[0];

The address of the 2> element a[0][0] is FFC1, so the address of the array a[0] is the same as the address of the element a[0][0], i.e. a[0] = &a[0][0];

3> can finally come to a conclusion: a = a[0] = &a[0][0], and so on, can be derived a[1] = &a[1][0]

3. Initialization of two-D arrays

* Initialize by line

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

* Initialize in order of storage (1th row, then 2nd row)

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

* Initialization of 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 people may wonder why the number of rows can be omitted, but the number of columns cannot be omitted. Others may ask, can you specify only the number of rows, but omit the number of columns?

In fact, the question is simple, if we write this:

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

As we all know, the two-dimensional array will first hold the 1th line of elements, because the number of indeterminate columns, that is, not sure how many elements of the 1th row, so there will be a lot of things here, maybe 1, 2 is the 1th row, or 1, 2, 3, 4 is the first row, or even 1, 2, 3 6 All of them belong to line 1th.

Three-dimensional or even more multidimensional arrays are no longer mentioned, and so on.

C language 08-arrays

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.