Two-dimensional arrays and pointers (C language)

Source: Internet
Author: User
Tags array definition

Two-dimensional arrays and pointers

The addresses of two-dimensional arrays and pointers 1, two-dimensional arrays and arrays of elements are defined as follows: int *p, a[3][4];

1) A two-dimensional array of two-dimensional arrays consisting of several one-dimensional arrays in the C language is actually a one-dimensional array, and each member of the one-dimensional array is a one-dimensional array. As defined in the a array, the visual A array consists of a[0], a[1], a[2] and other three elements, and a[0], a[1], a[2] and so on each element is composed of 4 integral elements of a one-dimensional array. You can use a[0][0], a[0][1], and so on to refer to each element in a[0], and so on. As explained in the second section, a one-dimensional array name defined in the function body or outside the function in the C language is an address constant whose value is the address of the first element of the array, and the base type of this address is the type of the element. In the above two-dimensional array, a[0], a[1], a[2] are all one-dimensional array names and also represent an immutable address variable whose value is the address of the first element in each row of a two-dimensional array, whose base type is the type of the array element. Therefore, for a two-dimensional array, an expression such as a[0]++ is illegal. If there is an expression a[0]+1, 1 of the units in the expression should be 2 bytes. In the above definition, the pointer variable p has the same base type as A[i] (0≤i<3), so the assignment statement p=a[i]; is legal. We know that a[i] can also be written as: * (A+i), so the above assignment can also be written as: p=* (A+i);.

2) Two-dimensional array name is also an address constant two-dimensional array name is also a pointer to hold the address constant, whose value is the address of the first element in a two-dimensional array. Above a array, the value of array name A is the same as the value of a[0], except that its base type is an array type with 4 integral elements. That is, the value of a+0 is the same as the value of a[0], the value of a+1 is the same as the value of a[1], and the a+2 value is the same as the value of a[2], which represents the first address of the 0th, first, and second rows in the A array, respectively. A two-dimensional array name should be interpreted as a row pointer. In the expression a+1, the unit of the value 1 should be 4x2 bytes instead of 2 bytes. The assignment statement p=a; is illegal because the base type of P and a is different. Similarly, for two-dimensional array name A, it is not possible to perform operations such as A++,a=a+i.

3) address of two-dimensional array elements the address of the two-dimensional array element can be evaluated by the expression &a[i][j] or by the first address of each line. In the above two-dimensional array A, the address of each element can be represented by the first address of each line: A[0], a[1], a[2], and so on. such as: Address &a[0][0] can be expressed in a[0]+0, address &a[0][1] can be expressed in a[0]+1, if 0≤i<3, 0≤j<4, then a[i][j] addresses can be evaluated by the following five types of expressions: (1) &a[i][j] (2) A [I]+j (3) * (A+i) +j (4) &a[0][0]+4*i+j (5) a[0]+ 4*i+j

In the expression above, the base type of a[i], &a[0][0], a[0] is of type int, and the system automatically determines that the unit of the constant 1 in the expression is 2 bytes. However, it is not possible to write an expression for A[i][j] address: a+4*i+j, because the base type of a is an array type of 4 integer elements, the system automatically determines the unit of constant 1 is 8 bytes.

2, by the address to refer to the two-dimensional array element if the following definition: int a[3][4],i,j; and when 0≤i<3, 0≤j<4, a array of elements can be referenced by the following five expressions:

(1) A[i][j] (2) * (A[I]+J) (3) * (* (a+i) +j) (4) (* (A+i)) [j] (5) * (&A[0][0]+4*I+J)

In (2), expression * (a[i]+j), because the base type of a[i] is int,j, the displacement amount is 2xj bytes.

In (3), the expression * (* (a+i) +j), the base type of a is an array of 4 elements, the displacement of I is 4x2xi bytes, while the base type of * (A+i) is int,j the displacement amount is still 2xj bytes.

In (4), a pair of parentheses outside of * (A+i) is indispensable, if written as: * (A+i) [j], because the operator [] has precedence over the * number, the expression can be converted to: * (* (a+i) +j)), that is: * (* (A+I+J)), then I+j will make the displacement of 4x2x j) Bytes, indicating that this is not the address of the element a[i][j]. * (* (A+I+J)) is equivalent to * (A[i+j]), equivalent to: a[i+j][0], refers to the array element a[i+j][0], rather than a[i][j], it is likely long past the scope of the array definition.

In (5), &a[0][0]+4*i+j represents the address of the array element A[i][j], and the expression * (&A[0][0]+4*I+J) represents the storage unit of the array element A[i][j] through the site operator * number.

3, by establishing an array of pointers to refer to two-dimensional array elements if the following definition: int *p[3], a[3][2], i,j; In this case, the specifier *p[3], also follows the precedence of the operator, a pair of [] precedence is higher than the * number, so P first and [] together, constitute p[3], Shows that P is an array name, and the system will open 3 contiguous storage units for it; the * number in front of it indicates that the array p is a pointer type, and that each element is a pointer to the base type int. If the condition is met: 0≤i<3, then p[i] and a[i] are the same base type, p[i]= a[i] is a valid assignment expression.

If there is the following loop: for (i=0; i<3; i++) p[i]= a[i]; here, the a[i on the right of the assignment is a constant, representing the first address of each row of a array, the p[i on the left of the assignment number is a pointer variable, the result of the loop execution makes P[0], p[1], p[2] Point to the beginning of each line of a array, respectively. At this point, the relationship between the array p and the group A is shown in 9.6.

When each element of the P array points to the beginning of each line of a array, the reference form of the A array element A[i][j] (a[i]+ j) and * (P[I]+J) is completely equivalent. Thus, the array of a elements can be referenced by the pointer array p as follows: (1) * (P[I]+J) (2) * (* (p+i) +j) (3) (* (P+i)) [j] (4) p[i][j] Different: The value in P[i] is variable, and a[i] The values in are immutable.

To refer to a two-dimensional array element by establishing a row pointer: int a[3][2], (*PRT) [2]; here, specifier (*PRT) [2], because of the existence of a pair of parentheses, the * number is first combined with PRT, stating that PRT is a pointer variable, and then the specifier [2 ], indicating that the base type of the pointer variable PRT is an array containing two int elements. Here, the base type of PRT is the same as a, so prt=a; is a valid assignment statement. Prt+1 equivalent to a+1, equivalent to a[1]. When PRT points to the beginning of an array of a, it can be referenced in the form of A[i][j]: (1) * (PRT[I]+J) (2) * (* (prt+i) +j) (3) (* (Prt+i)) [j] (4) prt[i][j] Here, PRT is a pointer variable whose value is variable , and A is a constant.

Report:

#include "stdio.h"
void disp (int (*a) [3])
{
printf ("%d", * (* (a+1) +1));
}

void Main ()
{
int a[2][3];
A[0][0]=1;
a[0][1]=2;
a[0][2]=3;
a[1][0]=4;
a[1][1]=5;
a[1][2]=6;
Disp (&a[0]);//You can also write disp (a);
}

Two-dimensional arrays and pointers (C language)

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.