C Language Review 3/24

Source: Internet
Author: User

1. One-dimensional arrays and pointers

int a[]; int *p = A;

A[i] <=>* (a+i) <=>* (p+i)

A as the array name of a one-dimensional array, its value is fixed. When the array name is used as a function parameter, the shape parameter group name is handled as a pointer variable. If you use a pointer variable as an argument, you must first make the pointer variable have a certain value, pointing to a defined object.

2. Two-d arrays and pointers

Define a two-dimensional array int a[3][4];

From a two-dimensional array perspective, a represents the address of the first element of a two-dimensional array, and now the first element is no longer a simple integer element, but a one-dimensional array consisting of 4 integral elements, so a represents the first address of the first row, and A + 1 represents the first address of the line with the ordinal 1. If the first row address of the two-dimensional array is 2000 and an integer data is 4 bytes, the value of a + 1 should be 2000 + 4*4 = 2016. A[0],A[1],A[2] Since it is a one-dimensional array name, and the C language specifies that the array name represents the address of the first element of the array, A[0] represents the address of the No. 0 column element in the one-dimensional array a[0], or &a[0][0]. a[i][j]=* (A[i] + j) =* (* (A + i) + j). If A is a one-dimensional array name, then A[i] represents the storage unit of an element of the array ordinal of I. A[i] is a physical address, which is the storage unit. But if a is a two-dimensional array, then a[i] is a one-dimensional array name, which is just an address and does not represent the value of an element (such as the same-dimensional array name is just a pointer constant). The values for a + 1 and * (A + 1) are 2016, but the meaning is different, A + 1 is the 1 header address, * (A + 1) equivalent to A[1], which represents the address of the 1 row 0 column element a[1][0].

A two-dimensional array name is a pointer to a row, and a one-dimensional array name is a pointer to a column. The pointer to the column is converted to a pointer to the row, preceded by A *. For example, a and a + 1 are pointers to rows, preceded by A * is *a and * (a+1), they become pointers to columns, pointing to an array of 0 rows 0 column elements and 1 rows 0 columns of elements. Conversely, the pointer to the column is preceded by a &, which becomes a pointer to the row. For example, A[0] is a pointer to a 0-row 0-column element, preceded by A & as &a[0], because a[0] is equivalent to * (a+0), so &a[0] is equivalent to &*a, which is equivalent to a, which points to 0 rows of a two-dimensional array. Do not take &a[i] as the physical address of the a[i] element, because there is no real data storage unit such as A[i]. It is just a method of calculating the address, which can get the first address of line I, the values of &a[i] and a[i] are the same, but their meanings are different. &a[i] or a+i points to the row, A[i] or * (A + i) to the column.

inta[3][4] = { {1,2,3,4},{1,2,3,4},{1,3,4,4} };int(*p) [4];//defines p as a pointer variable pointing to a one-dimensional array containing 4 integral elementsp =a;printf ("%d", * (* (P +1) +2));

3.

C Language Review 3/24

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.