A sub-question: pointer to a two-dimensional array... My understanding (int w [2] [3], (* PW) [3]; PW = W; then the following error is. * (W [0] + 2) B. * (PW + 1) [2] C .. PW [0] [

Source: Internet
Author: User

Int W [2] [3], (* PW) [3]; PW = W;
Which of the following is false?
A. * (W [0] + 2)
B. * (PW + 1) [2]
C. Pw [0] [0]
D. * (PW [1] + 2)

This evening I carefully studied the multi-dimensional array of C and the pointer to the multi-dimensional array (in the final analysis, these two items are completely consistent)
The above question is a two-dimensional question. After you understand this question, the multi-dimensional question will naturally pass...
To solve these problems, we need to have a deep understanding of the role of the three symbols "*, &, []" in operations on multi-dimensional arrays. Let's talk about them below.

Function in multi-dimensional array.
(1) *: Get the "things" stored in the corresponding pointer. (This is the most physical value in one dimension. In binary dimension, this is a pointer to a one-dimensional array, 3D... Multi-dimensional time ...).
(2) &: Get the address of the corresponding variable.
(3) []: The offset relative to the current pointer.This sentence is critical to understanding the meaning of the question.For example:
Example 1: For int A [5], a [3] indicates three offset positions relative to:
That is, a [3] = * (a + 3)

Example 2: For int A [5] [4], a [3] [2] indicates two offsets relative to a [3, A [3] indicates three offset positions relative to:
That is, a [3] [2] = * (a [3] + 2) = * (a + 3) + 2)

Example 3: For int A [5] [4] [3], then a [3] [2] [1] indicates that the offset from a [3] [2] is 1, A [3] [2] offsets two locations relative to a [3], while a [3] offsets three locations relative to:
That is, a [3] [2] [1] = * (a [3] [2] + 1) = * (a [3] + 2) + 1) = * (a + 3) + 2) + 1)

For more dimensions, and so on :....

The array name is a pointer constant. The case for a pointer variable is the same, for example:
For int A [5], * P, P = A; then a [3] = P [3] = * (p + 3 );
For int A [5] [4], (* P) [4], P =; then a [3] [2] = P [3] [2] = * (p [3] + 2) = * (p + 3) + 2 );

Note: The final form of the result obtained above is similar to: * (p + 3) + 2). The dimension in the innermost square brackets of this formula is the highest, when the internal brackets are expanded, the offset must multiply the weights of the dimensions (that is, the number of elements stored in each dimension). For example:

For int A [5] [4], (* P) [4], P =; then a [3] [2] = P [3] [2] = * (p [3] + 2) = * (p + 3) + 2) => * (p) + 3 × 4 + 2) = * (* P + 3 × 4 + 2)/P is a pointer to a two-dimensional array, * P is a pointer to one dimension. Need to understand this deeply...

For the above question, there is another knowledge point: [] has a higher priority than *, so
For Option B: * (PW + 1) [2] is equivalent to: * (PW + 1) [2]) according to the practice in Example 1 => * (PW + 1 + 2): * (PW + 3) = * (PW + 3) + 0) = * (PW [3] + 0) = pw [3] [0] = W [3] [0] (out of bounds ).
Note: For a [n], it indicates the value at the position (a + n), that is, a [n] = * (a + n ). So this question :( PW + 1) [2] = * (PW + 1 + 2)

for other options, refer to the above description of [], and use *, & Flexible conversion to determine whether to cross the border:
for. * (W [0] + 2) = W [0] [2] reference Example 2 (I .e: "A [3] [2] = * (a [3] + 2)") Inverse Operation
C. PW [0] [0] = W [0] [0]
D. * (PW [1] + 2) Same as Option A (because PW and W are essentially the same, the difference is that W is a pointer constant pointing to a one-dimensional array, PW is a pointer variable pointing to a one-dimensional array. To put it bluntly, W cannot be changed, while PW can only be changed.)

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.