A written question in C language: pointer and array Problems

Source: Internet
Author: User

1. array and pointer

It is easy to understand one-dimensional arrays.

Suppose the array int A [n],

And int * P;

Because we all know that the array name can actually represent the first address of the array.

P =;

Then we can use pointers to operate arrays.

* P = A [0];

* (P + 1) = A [1];

Then remove (), that is, * p + 1 = A [0] + 1. Because * has a higher priority than the binary operator +, the first element of the array is first obtained, then + 1;

* P ++ is actually a two-step operation. The P points to the value first, and then the pointer ++;

In fact, this expression is used in combination with the operator priority and operation order.

* The priority of ++ is at the same level, and both are from right to left. Therefore, P is first combined with ++, and * is combined with P.

However, P ++ only needs to wait until the entire expression ends and the P value is ++. Therefore, the correct description should be like this.

In the previous section, the P point value is obtained first, and then the pointer ++ only indicates that the logic is displayed on the surface.

However, this is totally different from taking P to the value first, and then P to the value ++.

This expression should be like this (* P) ++;

* P ++ is usually used to traverse elements in arrays through pointers.

Multi-dimensional arrays are a little complicated.

For example, int a [n] [N].

Array name at this time, the array name is the first address of the first element of array.

However, if we declare an int * P pointer, assign a value to P.

P = A is incorrect. P points to the address of an int type variable.

However, a is a two-dimensional array, and the first address indicated by a points to a (pointer to the int variable type ).

A pointer variable, that is, a points to a level more than P points.

Therefore, P = * A is correct in terms of type.

If a [0] is regarded as the whole, * a indicates the first address of a [0], and we know that a [0] Has n int-type elements, similarly, if we subdivide it, we can know that * A represents the address of the [0] [0] element. Then, for obtaining the first int element of a multi-dimensional array

We can use this to represent **,

Therefore, the first element of a [n] [N] can be expressed in this way.

A [0] [0] = ** A = * A [0];

Then how to traverse the elements in.

* (* A + I); 0 <= I <= n ^ 2;

For example, if I get 2, the expression means that * A obtains the first address of the element in a [0], and then offsets two digits,

It points to the third element a [0] [2] in a [0];

Then, what does ** A + I represent?

* The priority is higher than that of Binocular +. In fact (** A) + I;

It is easy to understand by adding brackets. What we always do is to take a [0] [0] And then + I.

So far, we can also predict a situation

** (A + I), at this moment, the pointer has shifted the I bit, in fact, from a [0] to a [0 + I];

Like a one-dimensional array, pointers are used to traverse array elements.

I try to apply for an int * P and use ++ to implement it.

Int * P = A [0] is actually the same as a one-dimensional array operation.

Tips; an important question is raised here.

We use a one-dimensional array as an example.

We know int * P =;

Then we can use P ++ to traverse the array.

Then, can I directly use a ++ to achieve location offset?

However, this is incorrect. After array a is determined, the first address of array a remains unchanged. Let us operate

A ++, that is to say, the first address of array a is offset by one digit. For a, a cannot be expressed.

The original array. Because your address is offset, you will lose an element storage space for the array,

Therefore, the data row clearly defines that arrays cannot be used as pointers ++ or --, And the compiler reports an error.

The above is a question that a friend encountered when he went to school. The problem of multi-dimensional array operations with pointers ~

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.